JavaScript/Handling JSON

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

[edit] Modern JSON Handling

Handling JSON may require adding a supporting library, which creates the global JSON object. This object is present natively only in new browsers (e.g. FF 3.5, IE8). Such a library can be found here: http://www.json.org/js.html

//Parsing JSON:

var myObject = JSON.parse(myJSONtext)


//Creating JSON:

var myJSONText = JSON.stringify(myObject);


[edit] Old way

In old browsers you could use the following syntax, but this raises issues of security, such as XSS.

var myObject = eval("(" + myJSONtext + ")")


[edit] More information