Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

What's the foreach equivalent in jQuery (JavaScript) to parse through a JSON object and return key/value pairs?

share|improve this question
4  
Put the question text in the question text, and make up a title that is just a title... – Guffa Mar 4 '10 at 14:04

1 Answer 1

up vote 11 down vote accepted

What do you mean by "JSON object"? JSON is a text format for serialising objects.

If you want to loop through the properties in an object that you got from deserialising a JSON string, you just loop through the property names:

for (key in object) {
  alert('key='+key+', value='+object[key]);
}

If you want to get the data from a JSON string, the easiest way is to parse it into an object. You can do that using an existing library, for example jQuery, or if you trust the contents of the string completely you can simply evaluate it:

var obj = eval(jsonString);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.