-1

我有一个 Json 对象,我想循环它。这是我的 JSON 对象

  [
    [
        {
            "adress": "8714 Pritchard Place 70118",
            "lat": "29.97646",
            "lng": "-90.13351"
        },
        {
            "adress": "p o box 13888 70185",
            "lat": "29.9639859",
            "lng": "-90.1220795"
        },
        {
            "adress": "3848 Cambaronne St 70118",
            "lat": "29.963181",
            "lng": "-90.11288230000002"
        },
        {
            "adress": "3023 Baudin St 70119",
            "lat": "29.9676822",
            "lng": "-90.098581"
        },
        {
            "adress": "400 N Galvez St 70119",
            "lat": "29.963966",
            "lng": "-90.087711"
        },
        {
            "adress": "2240 Louis St 70117",
            "lat": "29.9645507",
            "lng": "-90.0799022"
        },
        {
            "adress": "1554 Conti St # 9 70112",
            "lat": "29.9609508",
            "lng": "-90.074644"
        },
        {
            "adress": "1830 St. Phillip St. 70116",
            "lat": "29.967337",
            "lng": "-90.07302500000003"
        },
        {
            "adress": "1917 N Johnson St 70116",
            "lat": "29.972193",
            "lng": "-90.0674779"
        },
        {
            "adress": "1861 N. Galvez Street 70113",
            "lat": "29.9774886",
            "lng": "-90.063018"
        }
    ],
    [
        {
            "adress": "2603 St Anthony St 70119",
            "lat": "29.9836",
            "lng": "-90.063042"
        },
        {
            "adress": "2327 Aubry St 70119",
            "lat": "29.9830524",
            "lng": "-90.0698395"
        },
        {
            "adress": "832 N White St 70119",
            "lat": "29.9798109",
            "lng": "-90.07934879999999"
        },
        {
            "adress": "2753 Orleans Ave 70117",
            "lat": "29.972751",
            "lng": "-90.083087"
        },
        {
            "adress": "3801 Burxelles St., Apt A 70122",
            "lat": "29.992967",
            "lng": "-90.071667"
        },
        {
            "adress": "4332 Perlita St 70126",
            "lat": "29.998463",
            "lng": "-90.0761284"
        },
        {
            "adress": "5026 St Anthony Ave., Apt C 70122",
            "lat": "30.0046184",
            "lng": "-90.0759097"
        },
        {
            "adress": "5026 St. Anthony Ave Apt C 70122",
            "lat": "30.0092872",
            "lng": "-90.0649152"
        },
        {
            "adress": "3636 Frenchmen St 70122",
            "lat": "30.003136",
            "lng": "-90.0612"
        },
        {
            "adress": "3635 Frenchmen St. 70122",
            "lat": "29.995591",
            "lng": "-90.060578"
        },
        {
            "adress": "2666 Acacia St 70122",
            "lat": "29.9967787",
            "lng": "-90.0515892"
        },
        {
            "adress": "2701 Treasure St 70122",
            "lat": "29.993659",
            "lng": "-90.046275"
        },
        {
            "adress": "3245 Montegut St 70126",
            "lat": "29.995422",
            "lng": "-90.040324"
        },
        {
            "adress": "4221 Old Gentilly Rd 70126",
            "lat": "30.000224",
            "lng": "-90.044552"
        }
    ]
]
4

2 回答 2

1
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript">
        var jsonObject = [
            [
                {
                    "adress": "8714 Pritchard Place 70118",
                    "lat": "29.97646",
                    "lng": "-90.13351"
                },
                {
                    "adress": "p o box 13888 70185",
                    "lat": "29.9639859",
                    "lng": "-90.1220795"
                }
            ],
            [
                {
                    "adress": "2603 St Anthony St 70119",
                    "lat": "29.9836",
                    "lng": "-90.063042"
                },
                {
                    "adress": "2327 Aubry St 70119",
                    "lat": "29.9830524",
                    "lng": "-90.0698395"
                }
            ]
        ]
    </script>
</head>
<body>
    <script type="text/javascript">
        for(var i in jsonObject) {
            for(var j in jsonObject[i]) {
                for(var k in jsonObject[i][j]) {
                    document.writeln("key = "+k+", value = "+jsonObject[i][j][k]+"<br>");
                }
            }
        }
    </script>
</body>
</html>
于 2013-10-10T12:17:31.397 回答
0

首先,你有一个 Javascript 对象,JSON 是一种以可读形式存储 javascript 对象的方式。

它返回一个对象的原因是您使用 for 循环引用了一个对象。您给出的 for 循环:

for(i=0;i<javascript_array.length;i++) {
  alert(( javascript_array[i] ));
}

在这种情况下,javascript_array.length 将返回 2,因为您的第一个数组中有两个数组。您将需要遍历使用另一个 for 循环返回的数组,在该循环中,您将看到另一个包含您需要的值(地址、经度和纬度)的对象。这些也需要迭代才能检索它们。

于 2013-10-10T12:19:46.207 回答