Given a string of JSON data, how can you safely turn that string into a JavaScript object?
Obviously you can do this unsafely with something like...
var obj = eval("(" + json + ')');
...but that leaves us vulnerable to the json string containing other code, which it seems very dangerous to simply eval.
json.parse will change into object.
You also can use
reviver
function to filter.for more information read JSON.parse
Just for fun, here is the way using function :
JSON.parse() converts any JSON String passed into the function, to a JSON Object.
For Better understanding press F12 to open Inspect Element of your browser and go to console to write following commands : -
Now run the command :-
you'll get output as Object {result: true, count: 1}.
In order to use that Object, you can assign it to the variable let's say obj :-
Now by using obj and dot(.) operator you can access properties of the JSON Object.
Try to run the command
The jQuery method is now deprecated. Use this method instead:
Original answer using deprecated jQuery functionality:
If you're using jQuery just use:
It's exactly what you're looking for (see the jQuery documentation).
Edit: This answer is for IE < 7, for modern browsers check Jonathan's answer above.
Edit: This answer is outdated and Jonathan's answer above (
JSON.parse(jsonString)
) is now the best answer.JSON.org has JSON parsers for many languages including 4 different ones for Javascript. I believe most people would consider json2.js their goto implementation.