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

3 Answers 3

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
 });
Sign up to request clarification or add additional context in comments.

Comments

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.

4 Comments

You didn't try to run the code yourself, did you? JSON.parse throws an error because the value you pass is not a string.
@FelixKling Yes, you were right, I missed the quotes, thanks for pointing this out (I've updated the post).
Oh wow, I didn't realize this question was from 2012. Sorry, I would have just edited the answer myself instead of leaving an half-snarky comment :-/
@FelixKling No problem, it's ok.
0

This might help you.

http://underscorejs.org/#keys

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.