jQuery Parsing JSON Objects and Array

20,840

What you're after is simply this which is set at every iteration:

$.each(json_mc,function(){
    console.log(this);
});

Update

Given the raw response you've shown, you may need to decode it one more time:

$.each($.parseJSON(json_mc), function() {
    console.log(this);
});
Share:
20,840
Ganesh Yoganand
Author by

Ganesh Yoganand

Updated on July 09, 2022

Comments

  • Ganesh Yoganand
    Ganesh Yoganand almost 2 years

    In the following code:

    $.getJSON('<?php echo $this->Html->url(array('controller'=>'accounts', 'action'=>'get_customers_order_mcs')); ?>/'+customer_order_id,
                    function(data){
                        var json_mc = data.MasterCarton.mc;
                        alert(json_mc);
                        $.each(json_mc,function(){
                             console.log(this);
                       });
                 });
    

    The data sent as response is as follows-

    {
         "MasterCarton":{
                             "id":"40",
                             "mc":"[
                                        "1":{\"mc_number\":\"Warehouse1\\\/2013-2014\\\/CO\\\/ABC Corp\\\/239\\\/101-Red-1\",\"config\":{\"S2\":10,\"S1\":10},\"delivered\":0},            "2":{\"mc_number\":\"Warehouse1\\\/2013-2014\\\/CO\\\/ABC Corp\\\/239\\\/101-Red-2\",\"config\":{\"S2\":10,\"S1\":10},\"delivered\":0},                  "3":{\"mc_number\":\"Warehouse1\\\/2013-2014\\\/CO\\\/ABC Corp\\\/239\\\/104-Black-3\",\"config\":{\"S1\":7,\"S2\":7,\"S5\":6},\"delivered\":0},               "4":{\"mc_number\":\"Warehouse1\\\/2013-2014\\\/CO\\\/ABC Corp\\\/239\\\/104-Black-4\",\"config\":{\"S1\":7,\"S2\":7,\"S5\":6},\"delivered\":0},              "5":{\"mc_number\":\"Warehouse1\\\/2013-2014\\\/CO\\\/ABC Corp\\\/239\\\/104-Black-5\",\"config\":{\"S1\":6,\"S2\":6,\"S5\":7},\"delivered\":0}
                                   ]",
                             "delivery_note_id":"0",
                             "customer_order_id":"314"
                     }
    }
    

    The json array inside of mc is as shown below-

    [
       "1":{
          "mc_number":"Warehouse1\/2013-2014\/CO\/ABC Corp\/239\/101-Red-1",
          "config":{
             "S2":10,
             "S1":10
          },
          "delivered":0
       },
       "2":{
          "mc_number":"Warehouse1\/2013-2014\/CO\/ABC Corp\/239\/101-Red-2",
          "config":{
             "S2":10,
             "S1":10
          },
          "delivered":0
       },
       "3":{
          "mc_number":"Warehouse1\/2013-2014\/CO\/ABC Corp\/239\/104-Black-5",
          "config":{
             "S1":6,
             "S2":6,
             "S5":7
          },
          "delivered":0
       }
    ]
    

    I am trying to parse each object in it using jquery, and each object is as follows-

        {
       "mc_number":"Warehouse1\/2013-2014\/CO\/ABC Corp\/239\/101-Red-1",
       "config":{
          "S2":10,
          "S1":10
       },
       "delivered":0
    }
    

    To get the above object I using the following jquery code-

    $.each(json_mc,function(){
           // What should the code be so as to get each individual objects.         
    });
    

    That is each and every time of .each should get me-

       {
          "mc_number":"Warehouse1\/2013-2014\/CO\/ABC Corp\/239\/101-Red-1",
          "config":{
             "S2":10,
             "S1":10
          },
          "delivered":0
       }
    
  • Ganesh Yoganand
    Ganesh Yoganand about 11 years
    Thanks for the answer jack but when i tried with your solution I am getting [, {, ", m, c ........... so on. So can you plese suggest me the way in which the whole object from { to } is obtained.
  • Ja͢ck
    Ja͢ck about 11 years
    @GaneshYoganand It works fine here. The results you're getting could be due to code that you haven't shown.
  • Ganesh Yoganand
    Ganesh Yoganand about 11 years
    I am getting the log like this. String { 0= "m" } add_sales_invoice (line 294) String { 0= "c" } add_sales_invoice (line 294) String { 0= "_" } add_sales_invoice (line 294) String { 0= "n" }
  • Ja͢ck
    Ja͢ck about 11 years
    @GaneshYoganand You will have to share some of that code, I have no idea what you're doing with the data.
  • Ganesh Yoganand
    Ganesh Yoganand about 11 years
    Jack please look at the question I have edited. I need each objects of mc. What can I do?