Skip to content

Instantly share code, notes, and snippets.

@Flave
Created October 10, 2012 17:11
Show Gist options
  • Save Flave/3866989 to your computer and use it in GitHub Desktop.
Save Flave/3866989 to your computer and use it in GitHub Desktop.
toSource() function adds crazy parentheses around each object - why?
var testObj = {
"foo":[
{
"a" : 1,
"b" : 2,
"c" : [1, 2, 3, 4]
},
{
"a" : 1,
"b" : 2,
"c" : [1, 2, 3, 4]
}
]
}
var toSourced = testObj.toSource();
var stringObj = toSourced.replace(/\((.*)\)/, "$1");
alert(stringObj);
@ff6347
Copy link

ff6347 commented Oct 10, 2012

Ich denke es gibt 2 Möglichkeiten.

  1. Du schreibst den JSON String selber und speicherst ihn in einer Datei:
var my_name1 = "John"; 
var txt = '{ "employees" : [' +
'{ "firstName":"' + my_name1 + '" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}';   

Das kannst du dann mit eval() laden.
Guck mal hier
2. Du benutzt JSON.stringify(myObject, replacer); Dafür musst du aber eine externe JS Library laden. Beschrieben wird das hier und die lib gibt es hier

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment