0
{
    "status":"success",
    "data":
        {
            "title":"Testing",
            "token":"77fl",
            "questions":
                [{
                    "title":"How are you?",
                    "token":"UHGXI",
                    "kind":"multiple-choice",
                    "options":
                        [{
                            "content":"Good",
                            "token":"tLAzWd",
                            "kind":"text"
                         },
                    {
                          "content":"bad",
                          "token":"8LRR3t",
                          "kind":"text"
                     }]}]}}

I've never parsed JSON data before... what is a good first step sort of thing to parse this? I obviously don't need all of this, I need all instances of 'title', 'kind', and 'content'.

Do you do it similar to parsing a string?

This is my function to grab it...

$(document).ready(function(){
  $("button").click(function(){
    $.getJSON("http://quicksurvey.herokuapp.com/api/surveys/77fl/ask.json",function(result){
      });
    });
  }); 
});

Where is my returned data stored exactly? Is it in 'result'?

I'm very new to all of this so I apologize if I'm making some obvious mistakes. I've been looking around for answers to my question but can't find an easy introduction to this. I'd like to parse using javascript.

2

2 Answers 2

3

The answer is yes, it is stored inside of result. The jQuery function takes the returned JSON string and converts it back into a native javascript object that represents the said string. If you didn't return the json string with a name then the result variable will be the object itself. But in your case, you named the object data so you should access it like:

$(document).ready(function(){
  $("button").click(function(){
    $.getJSON("http://quicksurvey.herokuapp.com/api/surveys/77fl/ask.json",function(result){
         //result.data 
         //result.status
      });
    });
  }); 
});
Sign up to request clarification or add additional context in comments.

Comments

1

Yes you can, just parse it as an jSON object. You can access properties and arrays as a javascript object or properties. Fiddle

JS Version

for(var i =0; i<result.data.questions.length; i++)
{
    alert(result.data.questions[i].title + ' ' + result.data.questions[i].kind);
}

Jquery:-

alert(result.data.title);
$.each(result.data.questions,function(i,o){
    alert(o.title + ' ' + o.kind);
});

3 Comments

Why downvote? A comment would be useful, an anonymous down vote can't improve anything....
Not sure why you got a downvote but the looping method worked great for me! I appreciate the help. I wish I could give two people credit for helping me out
Thanks for who-ever reversed the downvote :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.