1

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."
                }
                }
                ]
Jeff
  • 12,555
  • 5
  • 33
  • 60
G.S
  • 257
  • 1
  • 4
  • 16
  • possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Felix Kling Apr 14 '14 at 06:36

3 Answers3

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
 });
Bogdan
  • 865
  • 6
  • 10
2

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.

Oscar Jara
  • 14,129
  • 10
  • 62
  • 94
0

This might help you.

http://underscorejs.org/#keys

var list=_.Keys(data["Restoration"][0]);
Shailesh
  • 492
  • 3
  • 9
  • 27