我怎么读这样的JSON结构?

时间:2010-07-07 00:44:56

标签: javascript json

{"some_id":
    [
    {"city":"Bellevue"},
    {"state":"Washington"}
    ]
}

4 个答案:

答案 0 :(得分:5)

var theJSonString = '({"some_id": [ {"city":"Bellevue"}, {"state":"Washington"} ] })';
var x = eval(theJSonString);
alert(x.some_id[0].city); // will display "Bellevue"

答案 1 :(得分:3)

var json = {"some_id": [ {"city":"Bellevue"}, {"state":"Washington"} ] }

json.some_id[0].city等于“Bellevue”

json.some_id[1].state等于“华盛顿”

答案 2 :(得分:0)

this (the json parser and stringifier from json.org)可能会有所帮助:)(请查看页面底部的链接)

答案 3 :(得分:0)

目前所有浏览器都支持window.JSON.parse()。它需要一个JSON格式的字符串并返回一个Javascript对象或数组。

演示:http://jsfiddle.net/ThinkingStiff/KnbAJ/

脚本:

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 />

输出:

length: 2
some_id[0].city: Bellevue
some_id[1].state: Washington