sreenivasulu
sreenivasulu

Reputation: 11

how to access json data through javascript

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

Answers (3)

Ricardo Murillo
Ricardo Murillo

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

jcubic
jcubic

Reputation: 66478

You can use

object = eval("(" + json_string + ")")

or

object = JSON.parse(json_string)`

Upvotes: 0

Adeel
Adeel

Reputation: 19228

use eval(json-object), if you trust the source, or you can use the json parser here

Upvotes: 0

Related Questions