gpt4 book ai didi

jQuery 解析 JSON 对象和数组

转载 作者:行者123 更新时间:2023-12-01 00:52:06 28 4
gpt4 key购买 nike

在下面的代码中:

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

作为响应发送的数据如下-

{
"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"
}
}

mc内部的json数组如下图-

[
"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
}
]

我尝试使用jquery解析其中的每个对象,每个对象如下-

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

要获取上述对象,我使用以下 jquery 代码-

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

这就是 .each 的每一次都应该让我——

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

最佳答案

您所追求的只是在每次迭代时设置的this:

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

更新

鉴于您显示的原始响应,您可能需要再解码一次:

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

关于jQuery 解析 JSON 对象和数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15738296/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com