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
}
asked Apr 1, 2013 at 5:48
Ganesh Yoganand
1531 gold badge3 silver badges9 bronze badges
1 Answer 1
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);
});
answered Apr 1, 2013 at 5:51
Ja͢ck
174k39 gold badges269 silver badges317 bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
Ganesh Yoganand
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.
Ganesh Yoganand
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
@GaneshYoganand You will have to share some of that code, I have no idea what you're doing with the data.
Ganesh Yoganand
Jack please look at the question I have edited. I need each objects of mc. What can I do?
lang-js