jQuery 解析 JSON 对象和数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15738296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:42:07  来源:igfitidea点击:

jQuery Parsing JSON Objects and Array

jqueryjsoneacharrays

提问by Ganesh Yoganand

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-

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

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

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

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

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

$.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-

那是每一次 .each 应该让我-

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

回答by Ja?ck

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

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

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