Veltar
Veltar

Reputation: 741

loop through json with multiple objects

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

Upvotes: 2

Views: 12913

Answers (4)

xkeshav
xkeshav

Reputation: 54022

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

Upvotes: 3

Kieron
Kieron

Reputation: 27107

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

Upvotes: 1

Alexander Kahoun
Alexander Kahoun

Reputation: 2488

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.

Upvotes: 0

MikeTheReader
MikeTheReader

Reputation: 4190

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

Upvotes: 0

Related Questions