jQuery Parsing JSON Objects and Array

arrays, each, jquery, json

Solution

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);
});

Problem

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 } ```

Original source