adi adi - 4 months ago 8
JSON Question

Error accessing json object data

I am not able to understand this. I have the below json data :

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


I want to access the data from the pincode_error node. I am running a loop to fetch the pincode_error data and display it in a div block as below.

$(".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>');


I tried to access it via below methods, but couldn't get through.


  1. response.data.pincode_error.i.id

  2. response.data.pincode_error[i].id



Can anyone help, where I am going wrong.

Answer

It depends on how are you iterating data.pincode_error.

data.pincode_error is not an array, so you need to iterate on its keys.

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