2013-04-01 113 views
3

在下面的代码:发送作为响应jQuery的解析JSON对象和阵列

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

那。每次的每一次应该得到的是我 -

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

回答

7

你后是什么简单地说是this,它是在每次迭代中设置的:

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

更新

鉴于还有你的原始响应,您可能需要将其解码一次:

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

感谢您的回答插孔,但是当我用您的解决方案试过我越来越[, {,“,m,c ...........等等。所以,你可以提出一个建议,告诉我从{到}的整个对象的获取方式。 –

+0

@ GaneshYoganand它工作正常[在这里](http://jsbin.com/uwijan/1/)。你得到的结果可能是由于你没有显示的代码。 –

+0

我得到这样的日志。 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”} –