2

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

1
  • 4
    Put the question text in the question text, and make up a title that is just a title... Commented Mar 4, 2010 at 14:04

1 Answer 1

12

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);
Sign up to request clarification or add additional context in comments.

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.