最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - getJSON to fetch data from this json array - Stack Overflow

programmeradmin4浏览0评论

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

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."
                }
                }
                ]
Share Improve this question edited Feb 2, 2012 at 0:41 Jeff 12.8k5 gold badges35 silver badges62 bronze badges asked Feb 2, 2012 at 0:21 G.SG.S 2571 gold badge4 silver badges16 bronze badges 1
  • possible duplicate of Access / process (nested) objects, arrays or JSON – Felix Kling Commented Apr 14, 2014 at 6:36
Add a ment  | 

3 Answers 3

Reset to default 2

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/#keys

var list=_.Keys(data["Restoration"][0]);
发布评论

评论列表(0)

  1. 暂无评论