G.S
G.S

Reputation: 257

getJSON to fetch data from this json array

This is a sample json array from my code. How can i use getJSON to fetch data from this array.

"Restoration": [
                {
                "Easy": {
                "value": "1",
                "info": "This is Easy."
                },
                "Medium": {
                "value": ".75",
                "info": "This is Medium."
                },
                "Difficult": {
                "value": ".5",
                "info": "This is Difficult."
                }
                }
                ]

Upvotes: 1

Views: 14896

Answers (3)

Oscar Jara
Oscar Jara

Reputation: 14187

This is an alternative to use "jQuery.getJSON()" because sometimes we don't have a "domain/file.json" or somewhere to do the $get or we don't want to use jQuery for this simple process.

This method parses json from string.

You can do it with simple javascript like this:

//json string for testing
var jsonstr = '{"id":"743222825", "name":"Oscar Jara"}';

//parse json
var data = JSON.parse(jsonstr);

//print in console
console.log("My name is: " + data.name + " and my id is: " + data.id);

Hope this helps.

Regards.

Upvotes: 2

Shailesh
Shailesh

Reputation: 492

This might help you.

http://underscorejs.org/#keys

var list=_.Keys(data["Restoration"][0]);

Upvotes: 0

Bogdan
Bogdan

Reputation: 865

using jQuery jQuery.getJSON():

 $.getJSON('ajax/test.json', function(data) {
     console.log(data); //see your data ( works in Chrome / FF with firebug)
     console.log(data["Restoration"][0]["easy"]["value"]) //should output 1
 });

Upvotes: 2

Related Questions