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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9105449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:36:43  来源:igfitidea点击:

getJSON to fetch data from this json array

javascriptjsonjquerygetjson

提问by G.S

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

这是我的代码中的示例 json 数组。我如何使用 getJSON 从这个数组中获取数据。

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

回答by Bogdan

using jQuery jQuery.getJSON():

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

回答by Oscar Jara

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.

这是使用“jQuery.getJSON()”的替代方法,因为有时我们没有“域/file.json”或其他地方可以执行 $get,或者我们不想在这个简单的过程中使用 jQuery。

This method parses json from string.

此方法从 string 解析 json

You can do it with simple javascript like this:

您可以使用这样的简单 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);

Hope this helps.

希望这可以帮助。

Regards.

问候。

回答by Shailesh

This might help you.

这可能对你有帮助。

http://underscorejs.org/#keys

http://underscorejs.org/#keys

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