0

我有下一个 JSON 对象,但我无法从中获取纬度和经度。在 JavaScript 中,我每次尝试都会收到未定义的错误:

{
    "response": {
        "date_ts": 1352514978,
        "raw": {
            "profile": "realtime",
            "battery": -100,
            "battery_state": "unknown"
        },
        "uuid": "b191d0d4-a967-47c9-b58b-3b491c8247db",
        "location": {
            "position": {
                "altitude": 0,
                "speed": -4,
                "horizontal_accuracy": 5,
                "latitude": 37.785834,
                "longitude": -122.406417,
                "heading": 0,
                "vertical_accuracy": 0
            },
            "type": "point"
        },
        "date": "2012-11-09T20:36:18-06:00"
    },
    "type": "onSuccess",
    "source": {}
}

有什么建议吗?

4

1 回答 1

2

您需要先解析 JSON,然后才能将其作为复杂的对象结构进行操作。

var parsed = JSON.parse(myjson);

console.log(parsed.response.location.position.longitude);

演示:http: //jsfiddle.net/VwWvP/

于 2012-11-10T03:13:40.613 回答