JSON in JavaScript

Aus Wikizone
Wechseln zu: Navigation, Suche

JSON Snippets[Bearbeiten]

 JSON.parse(myJson) // Object from JSON-String
 JSON.stringify(myJson) // JSON-String from Object (or Array)

 // get json and decode    
 myJson = JSON.parse(response);
 // add data
 myJson.checked = true;
 // send new json back
 $.post('/someurl/', JSON.stringify(myJson));


 // Speichern von JSON Strings in einem hidden Feld. Muss zusätzlich wegen der Gänsefüsschen etc. nochmal URL-Encoded werden:
 uriEncodedJson = encodeURIComponent( JSON.stringify( myJsonString ))
 $('#myHiddenField').attr('value',uriEncodedJson) 
 myJson = JSON.parse( decodeURIComponent( $('#myHiddenField').attr('value') ) )