24

How to iterate the json data in jquery.

[{"id":"856","name":"India"},
 {"id":"1035","name":"Chennai"},
 {"id":"1048","name":"Delhi"},
 {"id":"1113","name":"Lucknow"},
 {"id":"1114","name":"Bangalore"},
 {"id":"1115","name":"Ahmedabad"},
 {"id":"1116","name":"Cochin"},
 {"id":"1117","name":"London"},
 {"id":"1118","name":"New York"},
 {"id":"1119","name":"California"}
]

4 Answers 4

56

You can use $.each() like this:

$.each(data, function(i, obj) {
  //use obj.id and obj.name here, for example:
  alert(obj.name);
});
Sign up to request clarification or add additional context in comments.

3 Comments

What if I do not know the names of key and values from the JSON object?
what is i and obj in the arguments list?
@Adamnick I would hazard a guess that i is the index and obj is the object that makes up each element.
7

You can just use regular javascript too, which I think would be a bit faster (though I'm not really sure how jQuery optimizes each):

var data = [{"id":"856","name":"India"},
 {"id":"1035","name":"Chennai"},
 {"id":"1048","name":"Delhi"},
 {"id":"1113","name":"Lucknow"},
 {"id":"1114","name":"Bangalore"},
 {"id":"1115","name":"Ahmedabad"},
 {"id":"1116","name":"Cochin"},
 {"id":"1117","name":"London"},
 {"id":"1118","name":"New York"},
 {"id":"1119","name":"California"}
];

var data_length = data.length;
for (var i = 0; i < data_length; i++) {
  alert(data[i]["id"] + " " + data[i]["name"]);
}

edited to reflect Nick's suggestion about performance

1 Comment

It depends, accessing .length isn't trivial, so best to cache it if performance is a concern.
3

You could use the .each() function:

$(yourjsondata).each(function(index, element) {
    alert('id: ' + element.id + ', name: ' + element.name);
});

5 Comments

.each() is intended for operating on jQuery objects, which aren't meant to contain ordinary arrays, better to use $.each() as it was intended.
@Nick, I thought that jquery objects such as ones returned from jquery selectors were ordinary arrays. I agree with you that in this case the static $.each method would be more appropriate.
they have a bunch of other properties as well, you can use .get() (which internally is .toArray()) to get a base array of DOM elements though.
@Nick, yes I know that objects in this array are enhanced with additional properties and methods.
Perhaps a minor difference, but I think it would be more accurate to say that a jQuery object is an object enhanced with some properties from Array. Otherwise it implies that they have the full functionality of an Array.
3

iterate on all the object's properties with the $.each function. in each iteration you'll get the name/key and the value of the property:

$.each(data, function(key, val) {
  alert(key+ " *** " + val);
});

Comments

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.