2

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);     
});  
2
  • can u please paste you json data not the image, so that we can work on that. 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.com/questions/1241068/… Commented May 19, 2011 at 13:17

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

1 Comment

Very strange, it works in your demo, when I implement it I have an infinite amount of alerts that say "Undefined".
1

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

Comments

0

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

Comments

0

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.

4 Comments

I get an infinite number of popups, all saying undefined.
Veltar, since your example provided an alert inside a loop, I am only editing based on your example. Having an alert inside a loop will create a lot of messages, the same as the number in your array.
Rory, I'm sorry but your method is incorrect. The obj_records is the array, not the individual object in the array. Please reference the jquery api: api.jquery.com/jQuery.each
Now the OP has edited to include his JSON you are correct. I will remove my comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.