3
[
    [
        {
            "Id": 3,
            "Code": "232",
            "TicketImage": "0"
        }
    ],
    [
        {
            "Id": 1,
            "Code": "23",
            "TicketImage": "1"
        },
        {
            "Id": 2,
            "Code": "24",
            "TicketImage": "1"
        }
    ],
    []
]

I have this JSon object which I m trying to parse.

var res = jQuery.parseJSON(JSON.stringify(obj.Message));

              $.each(res, function (i, tickets) {
                  $.each(tickets, function (i, ticket) {
                      console.log($(this));
                  });
              });

When i try this i get zillions of popups with letters. but i want object.

How can i parse this JSON data?

UPDATE:

           $.each(res, function (i, tickets) {
                  $.each(tickets, function (i, ticket) { 
                      console.log(ticket.Code);
                      console.log(ticket.TicketImage);
                  });
              });

This did it.

10
  • $(this) will give you the object ? < inside the .each loop > unless I am missing something else :) hope this helps Commented Jul 15, 2012 at 7:42
  • obj.Message has the JSON data. Commented Jul 15, 2012 at 7:42
  • console.log(obj.message) to make sure the data is correct. then console.log(res) to make sure it parsed correctly. Commented Jul 15, 2012 at 7:48
  • @MatthewBlancarte yes, they are parsed correctly. Commented Jul 15, 2012 at 7:49
  • @Tats_innit still zillions of alert, all undefined. Commented Jul 15, 2012 at 7:49

2 Answers 2

2

Careful you are overriding the i variable :

$.each(res, function (i, tickets) {
                  $.each(tickets, function (j, ticket) {
                      console.log(this);
                      // as a full string
                      console.log(this.ticketId+' : '+JSON.stringify(this));

                  });
});

​Besides this small thing, your loop is correct, you don't need the jquery wrapper on your 'this'. in $.each(array,function(k,v){}) , the k variable is the key to retrieve the current object and v it's value so you could also access it by doing this :

$.each(res, function (i, tickets) {
                  $.each(res[i], function (j, ticket) {
                      console.log(res[i][j].ticketId+' : '+res[i][j]);
                      // which is the exact same thing as :
                      console.log.(ticket);
                      // and even this:
                      console.(this);

                  });
});

The same code using 'this' :

$.each(res, function (i, tickets) {
                  $.each(this, function (j, ticket) {
                      console.log(this.ticketId+' : '+this);
                  });
});

using each value :

$.each(res, function (i, tickets) {
                  $.each(tickets, function (j, ticket) {
                      console.log(ticket.ticketId+' : '+ticket);
                  });
});
Sign up to request clarification or add additional context in comments.

Comments

0

Try:

var res = jQuery.parseJSON(eval(obj.Message));

1 Comment

If obj.Message is a valid JSON string then there is absolutely no need to use evil

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.