Bing 映射 JSON 不使用 JSON.parse 进行解析

Bing Maps JSON not parsing with JSON.parse

本文关键字:JSON parse 映射 Bing      更新时间:2023-09-26

我正在尝试在Windows应用商店应用程序中使用Bing Maps进行反向地理编码。我的请求(使用 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]

我做错了什么?

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

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

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

对象的层次结构不能显示 - 它可以被遍历!- 你需要将其编码为可以显示的东西(例如HTML)。

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