0

I'm trying to get the values from a JSON file with "$.each" below but it doesn't give me any values with the $.each method I Use below, do anyone have any suggestions how I can make this work properly? I want to get information for each object [Question] with Question value and then answers for each question.

JSON:

[
    {
        "Question": "Question 1",
        "Answers": [
            {
                "Answers": "Answer 1",
                "Correct": false
            },
            {
                "Answers": "answer2",
                "Correct": true 
            }
        ]
    },
    {
        "Question": "Question 2",
        "Answers": [
            {
                "Answers": "An Answer",
                "Correct": false
            }
        ] 
    }
]

jQuery:

$(document).ready(function () {   
    $.get("data.php", function(data) {
        $.each(data, function(i, val) {
            var q = new Question(count++, val.Question);
            questions.push(q);
        });

        $.each(val.Answers, function(i, a) {
            q.addAnswer(a.Answers, a.Correct, q);
            questions.push(q);
        });
    });

Thankful for any help!


UPDATE:

$(document).ready(function () {   
    $.get("data.php?campaign_id=39&social_user_id=1192&edit=true", function(data){

        var questions = [];
        $.each(data, function(i, val) {
            var q = new Question(count++, val.Question);
            questions.push(q);

            $.each(val.Answers, function(i, a) {
                q.addAnswer(a.Answers, a.Correct, q);

            });
        });
    });
});
Kim
  • 1,128
  • 6
  • 21
  • 41

2 Answers2

3

You need to nest the loops if you want to access val from the second one. You seem to have misplaced the closing parenthesis.

var questions = [];
$.each(data, function(i, val) {
    var q = new Question(count++, val.Question);
    questions.push(q);

    $.each(val.Answers, function(i, a) {
        q.addAnswer(a.Answers, a.Correct, q);
    });
});
process(questions); // callback
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Thanks, I made an update in my question, whats happening now is that I can get "data" immediately after the $.get but in the each it breaks and I don't recieve any data, could it be that it takes to much time to get the data from the php so it breaks? – Kim Aug 18 '13 at 14:06
  • It's not "after" the `get`, but "in" the callback. Which `each` does break, what is the exception message? – Bergi Aug 18 '13 at 14:08
  • However, your `questions` variable seemed to be global so I guess you make the common mistake not to use a callback. Read [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) to learn about the asynchronity – Bergi Aug 18 '13 at 14:10
  • I get this error in the console: Uncaught TypeError: Cannot use 'in' operator to search for '211' in the JSON – Kim Aug 18 '13 at 14:11
  • Then please add that code in your update which does contain the `in` operator… – Bergi Aug 18 '13 at 14:12
  • Hm, I haven't defined any 'in' operator, seems like it's getting it from the jquery.min file – Kim Aug 18 '13 at 14:15
  • Then please trace the error back to your invocation of jQuery code. I don't think `$.each` does throw this. – Bergi Aug 18 '13 at 14:17
0

I manage it to work now!

This is the code i used:

   $.get("data.php",
   function(data) {
       $.each(data, function(q ,val) {
           var q = new Question(count++, val.Question);
           questions.push(q);
               $.each(val.Answers, function(a, val) {
            q.addAnswer(val.Answers, val.Correct, q);
           });
       });
   }, "json");
Kim
  • 1,128
  • 6
  • 21
  • 41