最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - loop through json with multiple objects - Stack Overflow

programmeradmin1浏览0评论

I have some json-code with has multiple objects in it, as such:

These are the values inside the object:

This is the json-code:

[{"pk_records_id":"34","record_artist":"Bouncing Souls","record_title":"How I Spent My Summer Vacation","record_added_date":"2011-05-05 17:36:34","record_category":"punkrock","record_price":"11.00","record_cover_link":"img\/Bouncing Souls-How I Spent My Summer Vacation.jpg","record_amount_sold":null,"record_amount_stock":"400","record_description":"A great follow-up to Hopeless Romantic"},{"pk_records_id":"4","record_artist":"Descendents","record_title":"Everything Sucks","record_added_date":"2011-03-11 00:00:00","record_category":"punkrock","record_price":"12.00","record_cover_link":"img\/descendents_everything_sucks.jpg","record_amount_sold":"3124","record_amount_stock":null,"record_description":null}]

And this is the code I try to use, so I would be able to retrieve the values (obviously):

success: function(obj_records){
    $.each(obj_records, function(index, value) {
        alert(obj_records.index.pk_records_id); 
    });             
} 

But this doesn't work. How can I retrieve the data?

Edit:

If I use this code, I get an array for every single character in my json-code.

$.each(obj_records, function(index, value) {        
    alert(index + " : " + value);     
});  

I have some json-code with has multiple objects in it, as such:

These are the values inside the object:

This is the json-code:

[{"pk_records_id":"34","record_artist":"Bouncing Souls","record_title":"How I Spent My Summer Vacation","record_added_date":"2011-05-05 17:36:34","record_category":"punkrock","record_price":"11.00","record_cover_link":"img\/Bouncing Souls-How I Spent My Summer Vacation.jpg","record_amount_sold":null,"record_amount_stock":"400","record_description":"A great follow-up to Hopeless Romantic"},{"pk_records_id":"4","record_artist":"Descendents","record_title":"Everything Sucks","record_added_date":"2011-03-11 00:00:00","record_category":"punkrock","record_price":"12.00","record_cover_link":"img\/descendents_everything_sucks.jpg","record_amount_sold":"3124","record_amount_stock":null,"record_description":null}]

And this is the code I try to use, so I would be able to retrieve the values (obviously):

success: function(obj_records){
    $.each(obj_records, function(index, value) {
        alert(obj_records.index.pk_records_id); 
    });             
} 

But this doesn't work. How can I retrieve the data?

Edit:

If I use this code, I get an array for every single character in my json-code.

$.each(obj_records, function(index, value) {        
    alert(index + " : " + value);     
});  
Share Improve this question edited Jun 3, 2013 at 11:57 Chris 2,9401 gold badge32 silver badges44 bronze badges asked May 19, 2011 at 13:08 VeltarVeltar 7412 gold badges15 silver badges30 bronze badges 2
  • can u please paste you json data not the image, so that we can work on that. – xkeshav Commented May 19, 2011 at 13:12
  • still u use images? why don't you write manually, is this homework?? see the similar question : stackoverflow./questions/1241068/… – xkeshav Commented May 19, 2011 at 13:17
Add a ment  | 

4 Answers 4

Reset to default 3

try with

for (var i=0; i<json.length; i++) {
   alert("JSON Data: " + json[i].pk_records_id);
  // you need to write each key name here
}

DEMO

From the looks of it, you're accessing the result incorrectly.

Try:

success: function(obj_records){
    $.each(obj_records, function(index, value) {
        alert(value.pk_records_id); 
    });             
} 

I'm not sure (not in a place to test it), but I think you want:

success: function(obj_records){
    $.each(obj_records, function(index, value) {
        alert(value.pk_records_id); 
    });             
} 

I beleive what you want is this:

success: function(obj_records){
    $.each(obj_records, function(index, value) {        
        alert(value.pk_records_id);     
    });             
} 

the value parameter inside the function header is the individual object of the obj_records array that you are looping over.

发布评论

评论列表(0)

  1. 暂无评论