从多维json对象获取数据

时间:2022-12-23 20:35:07

I am trying to get the result of the following json object returned by google driving api.

我试图得到谷歌驾驶api返回的以下json对象的结果。

"routes": [
    {
        "bounds": {
            "Ga": {
                "C": 39.01115,
                "j": 39.06483
            },
            "xa": {
                "j": -77.57857000000001,
                "C": -77.47601
            }
        },
        "copyrights": "Map data ©2015 Google",
        "legs": [
            {
                "distance": {
                    "text": "9.4 mi",
                    "value": 15096
                },
                "duration": {
                    "text": "15 mins",
                    "value": 870
                },
                "end_address":"1234 Any St USA".....//and so on

how would i go about getting the data in "text" or "end_address"

我将如何获取“text”或“end_address”中的数据

i have tried

我试过了

 alert(result.routes.legs.distance.text)

and

 alert(result.routes.legs.end_address);

and i get

我明白了

Uncaught TypeError: Cannot read property 'distance' of undefined

and

Uncaught TypeError: Cannot read property 'end_address' of undefined

respectively

1 个解决方案

#1


routes is an array and legs is an array within each of the routes array objects

routes是一个数组,leg是每个路径数组对象中的一个数组

Try

 alert(routes[0].legs[0].distance.text)

#1


routes is an array and legs is an array within each of the routes array objects

routes是一个数组,leg是每个路径数组对象中的一个数组

Try

 alert(routes[0].legs[0].distance.text)