0

我正在尝试在 Windows 应用商店应用程序中使用 Bing 地图进行反向地理编码。我的请求(使用 WinJS.xhr)通过就好了,我从他们的示例页面得到了与此类似的响应:

{
   "authenticationResultCode":"ValidCredentials",
   "brandLogoUri":"http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png",
   "copyright":"Copyright © 2011 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.",
   "resourceSets":[
      {
         "estimatedTotal":1,
         "resources":[
            {
               "__type":"Location:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1",
               "bbox":[
                  47.636705672917948,
                  -122.137016420622,
                  47.6444311080593,
                  -122.1217297861384
               ],
               "name":"1 Microsoft Way, Redmond, WA 98052",
               "point":{
                  "type":"Point",
                  "coordinates":[
                     47.640568390488625,
                     -122.1293731033802
                  ]
               },
               "address":{
                  "addressLine":"1 Microsoft Way",
                  "adminDistrict":"WA",
                  "adminDistrict2":"King Co.",
                  "countryRegion":"United States",
                  "formattedAddress":"1 Microsoft Way, Redmond, WA 98052",
                  "locality":"Redmond",
                  "postalCode":"98052"
               },
               "confidence":"Medium",
               "entityType":"Address",
               "geocodePoints":[
                  {
                     "type":"Point",
                     "coordinates":[
                        47.640568390488625,
                        -122.1293731033802
                     ],
                     "calculationMethod":"Interpolation",
                     "usageTypes":[
                        "Display",
                        "Route"
                     ]
                  }
               ],
               "matchCodes":[
                  "Good"
               ]
            }
         ]
      }
   ],
   "statusCode":200,
   "statusDescription":"OK",
   "traceId":"99b1256e09044490bce82bbbba1dab7a"
}

但是,当我在数据上调用 JSON.parse 并尝试显示它时,它返回的只是

[object Object]

我究竟做错了什么?

4

1 回答 1

1

正如其他人所提到的,它确实解析了,你只是不认识结果。

JSON是将对象层次结构序列化为字符串。

JSON.parse(...)将 JSON 序列化(字符串)转换回对象层次结构。

无法显示对象的层次结构——它可以被遍历!-- 您需要将其编码为可以显示的内容(例如 HTML)。

为此,您需要遍历对象层次结构以构建 HTML 片段字符串。innerHTML然后,您可以通过现有元素(例如容器 DIV)的属性简单地将其添加到 HTML DOM 。

于 2013-09-13T04:06:23.593 回答