How would I read such a JSON structure?
{"some_id":
[
{"city"开发者_运维技巧:"Bellevue"},
{"state":"Washington"}
]
}
var theJSonString = '({"some_id": [ {"city":"Bellevue"}, {"state":"Washington"} ] })';
var x = eval(theJSonString);
alert(x.some_id[0].city); // will display "Bellevue"
var json = {"some_id": [ {"city":"Bellevue"}, {"state":"Washington"} ] }
json.some_id[0].city
equals "Bellevue"
and
json.some_id[1].state
equals "Washington"
And this (the json parser and stringifier from json.org) might help :) (check the link at the bottom of the page)
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
精彩评论