I have JSON returned from my server like:
{
"user": {
"nickname": "Ann",
"width": 90,
"height": 60
},
"user": {
"nickname": "Dan",
"width": 60,
"height": 90
}
}
I need to iterate thru all users and for example alert thare nicknames so I try next code:
var obj = jQuery.parseJSON('{ "user": { "nickname": "ole", "width": 333, "height": 222 }, "user": { "nickname": "jak", "width": 333, "height": 222 }}');
for (user in obj ) {
alert( obj[user][nickname]);
}
Which fails. What shall I do to alert all user names (I cant change how server returns json=( so I hope to find solution on JS side.)
Code is dying alive here.
Honestly, if you want to do this reliably, you can modify an existing pure JS JSON parser. For instance, take a look at Douglas Crockford’s simple recursive descent parser “parse_json”: https://github.com/douglascrockford/JSON-js/blob/master/json_parse.js
At line 272 (in the object function) we have:
We could replace this with:
Of course, around line 259 we’d have to add the variable dupKeys,
And then json_parse would parse your JS.