简体   繁体   中英

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."
                }
                }
                ]

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
 });

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.

This might help you.

http://underscorejs.org/#keys

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM