访问json对象数据时出错

时间:2016-07-16 08:10:51

标签: javascript jquery json

我无法理解这一点。我有以下json数据:

 {
     "data": {
         "pincode": "909090",
         "assembly_check": "1",
         "serviceable_check": "1",
         "part_pay_check": "0",
         "delivery_date": null,
         "tentative_delivery_date": {
             "start_day": "Thu,Jan 01",
             "end_day": "Thu,Jan 01"
         },
         "delivery_info": {
             "1133168": {
                 "delivery_date": null,
                 "tentative_delivery_date": {
                     "start_day": "Thu, Jan 01",
                     "end_day": "Thu, Jan 01"
                 }
             },
             "1133167": {
                 "delivery_date": null,
                 "tentative_delivery_date": {
                     "start_day": "Thu, Jan 01",
                     "end_day": "Thu, Jan 01"
                 }
             },
             "1127813": {
                 "delivery_date": null,
                 "tentative_delivery_date": {
                     "start_day": "Thu, Jan 01",
                     "end_day": "Thu, Jan 01"
                 }
             },
             "1104823": {
                 "delivery_date": null,
                 "tentative_delivery_date": {
                     "start_day": "Thu, Jan 01",
                     "end_day": "Thu, Jan 01"
                 }
             },
             "1104830": {
                 "delivery_date": null,
                 "tentative_delivery_date": {
                     "start_day": "Thu, Jan 01",
                     "end_day": "Thu, Jan 01"
                 }
             }
         },
         "pincode_error": {
             "1133168": {
                 "id": 1133168,
                 "name": "Ergonomic Chairs by Chromecraft",
                 "image": "e\/r\/90x99\/ergonomic-chairs-by-chromecraft-ergonomic-chairs-by-chromecraft-qaez66.jpg",
                 "url": "ergonomic-chairs-by-chromecraft-1133168.html"
             },
             "1133167": {
                 "id": 1133167,
                 "name": "Ergonomic Chairs by Chromecraft",
                 "image": "e\/r\/90x99\/ergonomic-chairs-by-chromecraft-ergonomic-chairs-by-chromecraft-7psbku.jpg",
                 "url": "ergonomic-chairs-by-chromecraft-1133167.html"
             },
             "1127813": {
                 "id": 1127813,
                 "name": "Ergonomic Chairs by Chromecraft",
                 "image": "e\/r\/90x99\/ergonomic-chairs-by-chromecraft-ergonomic-chairs-by-chromecraft-v9brdn.jpg",
                 "url": "ergonomic-chairs-by-chromecraft-1127813.html"
             },
             "1104823": {
                 "id": 1104823,
                 "name": "Ergonomic Chairs by Emperor",
                 "image": "e\/r\/90x99\/ergonomic-chairs-by-emperor-ergonomic-chairs-by-emperor-z5umi5.jpg",
                 "url": "ergonomic-chairs-by-emperor-1104823.html"
             },
             "1104830": {
                 "id": 1104830,
                 "name": "Student 704 Series Chair by Emperor",
                 "image": "e\/m\/90x99\/emperor-student-chair---704-series-emperor-student-chair---704-series-gq6v1r.jpg",
                 "url": "student-704-serieschair-by-emperor-1104830.html"
             }
         },
         "product": {
             "1133168": {
                 "id": 1133168,
                 "name": "Ergonomic Chairs by Chromecraft",
                 "image": "e\/r\/90x99\/ergonomic-chairs-by-chromecraft-ergonomic-chairs-by-chromecraft-qaez66.jpg",
                 "url": "ergonomic-chairs-by-chromecraft-1133168.html"
             },
             "1104823": {
                 "id": 1104823,
                 "name": "Ergonomic Chairs by Emperor",
                 "image": "e\/r\/90x99\/ergonomic-chairs-by-emperor-ergonomic-chairs-by-emperor-z5umi5.jpg",
                 "url": "ergonomic-chairs-by-emperor-1104823.html"
             }
         },
         "pincode_header": "Item(s) cannot be delivered <u>here<\/u>",
         "ships_together": {
             "status": false,
             "count": 0,
             "delivery_date": false,
             "date_range": false,
             "pids": [],
             "pidImages": [],
             "mode_of_transport": "",
             "location_id": 0,
             "ttsd": 0
         },
         "is_cod_exists": false,
         "assembly_header": "Assembly not provided <u>here<\/u>",
         "additional_requirement": {
             "template": "not required"
         },
         "exchange_pincode": null,
         "is_exchange_pincode_servicable": 0
     }
 }

我想从 pincode_error 节点访问数据。我正在运行一个循环来获取pincode_error数据并将其显示在div块中,如下所示。

$(".tp-btn").prepend('<div class="os-box jsonprds">'+
                    '<div class="os-box-hd clearfix">'+
                            '<div class="os-box-img">'+
                                '<a href="#"><img class="img-resp" id="prdimg" src="'+response.data.pincode_error[i].image+'" alt=""></a>'+
                            '</div>'+
                            '<div class="os-box-desc">'            +                  
                                '<p class="os-sub-txt hidden-txt"><a href="#" id="prdname">'+response.data.pincode_error[i].id.name+'</a> </p><div class="os-box-oos-del">'+
                                    '<a href="javascript: void(0)" class="os-box-del-each" id="'+response.data.pincode_error[i].id+'"><span class="icon ic-ck-delete"></span> <span class="os-box-oos-txt">Delete</span></a>'+
                                    '<a href="javascript: void(0)" class="os-box-wslist-each non_del" id="'+response.data.pincode_error[i].id+'"><span class="icon ic-favrite-o t-dgr"></span> <span class="os-box-oos-txt">Wishlist</span></a>'+
                                 '</div>'+
                            '</div>'+                            
                        '</div>'+
                    '</div>');

我尝试通过以下方法访问它,但无法通过。

  1. response.data.pincode_error.i.id
  2. response.data.pincode_error [I] .ID
  3. 任何人都可以帮忙,我出错了。

1 个答案:

答案 0 :(得分:1)

这取决于你如何迭代data.pincode_error

data.pincode_error不是数组,因此您需要迭代其键。

for ( var i in data.pincode_error )
{
 //all your code
}