{"some_id":
[
{"city":"Bellevue"},
{"state":"Washington"}
]
}
Asked
Active
Viewed 388 times
3

ThinkingStiff
- 64,767
- 30
- 146
- 239

Corel
- 31
- 1
-
11"open curly bracket, quote, some ID, colon, open square bracket, open curly bracket, quote, city, quote, colon, quote, Bell View, close curly bracket, comma, open curly bracket, quote, state, quote, colon, quote, Washington, quote, close curly bracket, close square bracket, close curly bracket." :D – Russell Jul 07 '10 at 00:47
-
See [Alternatives to JavaScript eval() for parsing JSON](http://stackoverflow.com/questions/945015/alternatives-to-javascript-eval-for-parsing-json) and [Safely turning a JSON string into an object](http://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object) for a number of methods – Michael Mrozek Jul 07 '10 at 00:48
4 Answers
5
var theJSonString = '({"some_id": [ {"city":"Bellevue"}, {"state":"Washington"} ] })';
var x = eval(theJSonString);
alert(x.some_id[0].city); // will display "Bellevue"

DanC
- 8,595
- 9
- 42
- 64
-
1If you parse JSON with `eval`, you need to wrap the contents in parentheses (e.g. `eval('(' + theString + ')')`), otherwise it gets parsed as a block statement instead of an object literal. – Matthew Crumley Jul 07 '10 at 02:02
-
3
var json = {"some_id": [ {"city":"Bellevue"}, {"state":"Washington"} ] }
json.some_id[0].city
equals "Bellevue"
and
json.some_id[1].state
equals "Washington"

Germán Rodríguez
- 4,284
- 1
- 19
- 19
0
All current browsers support window.JSON.parse()
. It takes a JSON formatted string and returns a Javascript object or array.
Demo: http://jsfiddle.net/ThinkingStiff/KnbAJ/
Script:
var json = '{"some_id":[{"city":"Bellevue"},{"state":"Washington"}]}'
object = window.JSON.parse( json );
document.getElementById( 'length' ).textContent = object.some_id.length;
document.getElementById( 'city' ).textContent = object.some_id[0].city;
document.getElementById( 'state' ).textContent = object.some_id[1].state;
HTML:
length: <span id="length"></span><br />
some_id[0].city: <span id="city"></span><br />
some_id[1].state: <span id="state"></span><br />
Output:
length: 2
some_id[0].city: Bellevue
some_id[1].state: Washington

ThinkingStiff
- 64,767
- 30
- 146
- 239
0
And this (the json parser and stringifier from json.org) might help :) (check the link at the bottom of the page)

Dan Heberden
- 10,990
- 3
- 33
- 29