这是我的代码中的示例json数组。如何使用getJSON从此数组中获取数据。
"Restoration": [
{
"Easy": {
"value": "1",
"info": "This is Easy."
},
"Medium": {
"value": ".75",
"info": "This is Medium."
},
"Difficult": {
"value": ".5",
"info": "This is Difficult."
}
}
]
答案 0 :(得分:2)
使用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
});
答案 1 :(得分:2)
这是使用“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);
希望这有帮助。
问候。
答案 2 :(得分:0)