Reputation: 11
I am developing JMAKI widgets,my task is to display the json data in a html page through javascript.
What's the best way to access the json data through javascript?
Upvotes: 1
Views: 3213
Reputation: 2865
if you have the json object already just access as you would with a normal object:
// Creating the JSON object from string
var obj = JSON.parse('{"attr1": 10, "attr2": "Some value"}');
// You can access it like this
var a = obj.attr1;
var b = obj.attr2;
console.log(a, b); // Will print 10, "Some value"
For me that is the best way to access a JSON object (not the only one) it feels more natural for me.
Upvotes: 2
Reputation: 66478
You can use
object = eval("(" + json_string + ")")
or
object = JSON.parse(json_string)`
Upvotes: 0