getJSON从这个json数组中获取数据
这是我的代码中的示例json数组。 如何使用getJSON从此数组中获取数据。
"Restoration": [ { "Easy": { "value": "1", "info": "This is Easy." }, "Medium": { "value": ".75", "info": "This is Medium." }, "Difficult": { "value": ".5", "info": "This is Difficult." } } ]
使用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 });
这是使用“jQuery.getJSON()”的替代方法,因为有时我们没有“domain / file.json”或者在某处执行$ get或者我们不想在这个简单的过程中使用jQuery。
此方法从字符串解析json 。
你可以用这样简单的javascript来做到这一点:
//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);
希望这可以帮助。
问候。