Nick
Nick

Reputation: 16095

Iterate JSON string

I have a JSON string like this

{"3560" : "something", "3980" : "something", "4580" : "1456"}

How to get data above as "key -> value" in javascript(jquery) ?

Upvotes: 1

Views: 700

Answers (2)

amurra
amurra

Reputation: 15401

var obj = jQuery.parseJSON(jsonObj);

Then you could access the data like obj.3560 or obj.3980 or iterate over them using a for-in loop like in Felix Kling's answer.

This requires jquery 1.4.1 or later to work.

Upvotes: 1

Felix Kling
Felix Kling

Reputation: 816364

var obj = JSON.parse(jsonString);

Now you can access obj["3560"], etc.

Or iterating:

for(var key in obj) {
    // do something with obj[key]
}

Upvotes: 5

Related Questions