getJSON从这个json数组中获取数据

getJSON to fetch data from this json array

本文关键字:获取 数据 数组 json getJSON      更新时间:2023-09-26

这是我的代码中的一个示例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);

希望这能有所帮助。

谨致问候。

这可能会对您有所帮助。

http://underscorejs.org/#keys

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