从json字符串中获取数据

时间:2013-08-29 02:51:40

标签: javascript jquery json

我是一个非常大的n00b javascript和jquery所以我在这里尽我所能。我从Stackmob查询返回JSON。我需要知道如何从此JSON字符串中正确获取namedistancelatlon,以便我可以在google api调用中进一步使用它们。< / p>

[
    {
        "name": "eBay",
        "lat_long": {
            "lon": -121.927551,
            "lat": 37.29546,
            "distance": 0.004635090828588474
        },
        "locations_id": "a3563711ebb44a088ae5ab2bd7ff2e0e"
    },
    {
        "name": "Facebook",
        "lat_long": {
            "lon": -121.894955,
            "lat": 37.339386,
            "distance": 0.005523591746943001
        },
        "locations_id": "3fbee8e363bf4247b2e8ca0958c38f4e"
    },
    {
        "name": "Yahoo",
        "lat_long": {
            "lon": -122.025159,
            "lat": 37.417237,
            "distance": 0.006310220058572611
        },
        "locations_id": "d0c568a6322f4a3b8d2fe30cba68f8f1"
    },
    {
        "name": "Hewlett Packard",
        "lat_long": {
            "lon": -122.145989,
            "lat": 37.415515,
            "distance": 0.006312489443257717
        },
        "locations_id": "f03676c3469d4b978bfefcb2ab13790d"
    },
    {
        "name": "Google",
        "lat_long": {
            "lon": -122.083979,
            "lat": 37.421931,
            "distance": 0.006351811575525541
        },
        "locations_id": "56325f71322a4c70ab0e3f1b3fb7fd9b"
    }
]

我的返回对象是model,我尝试通过循环模型[i] .name等进行引用,但在firebug中获取空字符串。

1 个答案:

答案 0 :(得分:8)

你可以这样做:

for(var i = 0; i < model.length; i++ ) {
    console.log( model[i].name ); //shows eBay, Facebook, etc
    console.log( model[i].lat_long.lat ); //gives value of lat
    console.log( model[i].lat_long.distance ); //gives distance value for each
}

演示jsFiddle