2011-04-04 103 views
0

我试图检索作为自定义参数传递到Bing地图REST地理位置的jsonso状态对象的值(http://dev.virtualearth.net/REST/V1 /位置/ ...)。我可以在响应方法调用(使用网络监视器)中看到作为第二个参数返回的值,但似乎无法在代码中访问它。下面是回归方法看起来像(摘录):Bing Maps REST API - 如何检索自定义参数

jsonp1301929429652({ “authenticationResultCode”: “ValidCredentials”,...更多数据... “traceId”: “41a7523951964ae9a76ca4ea91c4e80f | CH1M001467 | 02.00.82.2800 | CH1MSNVM001383” },“我的自定义值”

我试图把它捡起来的匿名方法,像这样:

$.getJSON(geocodeRequest, function (d, customParam) { 

    if (d != null && d.resourceSets.length > 0) { 
     var coordinates = d.resourceSets[0].resources[0].point.coordinates; 
     var loc = new Microsoft.Maps.Location(coordinates[0], coordinates[1]); 
     .... 
    } 
} 

不幸的是,customParam永远是不确定的。我怎样才能得到这个价值?

好了,这是一个可测试的例子:

$(function() { 
      var mapOptions = { 
       credentials: "Your API Key" 
      } 
      var geocodeRequest = "http://dev.virtualearth.net/REST/v1/Locations/US/Your State Abbrev/Your Zip/Your City/Your Street Address?jsonso=ABC123&jsonp=?&key=" + mapOptions.credentials; 

      $.getJSON(geocodeRequest, function (d, arg1, arg2, arg3) { 
       if (d != null && d.resourceSets.length > 0) { 
        var readMyCustomParam = arg1; 
       } 
      }); 
     }); 

如果上破的getJSON内的第一行请求您可以监控流量,你会看到该请求的结果正确返回jsonso参数。然后我的问题是在javascript中阅读。这样做的全部目的是循环遍历经销商列表,异步地理定位,然后使用返回的标识符查找数组中的经销商以加载它的描述等等。

+0

这是[链接](http://msdn.microsoft.com/zh-cn/library/ff701701。aspx)引用此REST方法 – Corgalore 2011-04-26 20:37:20

回答

0

@Corgalore ,对于延误抱歉,我可能应该使用更频繁使用的电子邮件进行注册。如果这还没有得到解决。我看到你的价值是回来了,但它不能很好地形成

每JSLint.com:

错误:

问题在第1行字符568:预期'(结束)',而是看到','。

{ “authenticationResultCode”: “ValidCredentials”, “brandLogoUri”: “HTTP:\/\/dev.v ...

JSON:坏

{"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":0, 
    "resources":[]}], 
    "statusCode":200, 
    "statusDescription":"OK", 
    "traceId":""}, 
    "ABC123" 

删除,” ABC123 “和JSLint说:”JSON:好“

看来你必须使用自定义分析器来获得该值......或者至少是一个更宽容的解析器除非我做错了什么,jsonParse使用jQuery在这个字符串上不适合我。

+0

Corgalore,你在这方面的决议是什么? – Jordan 2011-06-06 23:54:51

+0

抱歉,延迟。你是对的。如果不创建自定义分析例程,则无法获得该值。从Bing Maps API返回的数据仅在传递自定义参数时不是很好的JSON。 – Corgalore 2011-09-13 21:14:30

+0

没问题。感谢您的跟踪确认! – Jordan 2011-09-15 23:50:16

0

除非您使用自定义分析方法,否则它显示您没有返回有效的JSON(http://www.jsonlint.com/)。知道该自定义值是否为DateTime也很重要,因为这需要.NET中的特殊处理。

+0

返回的JSON来自映射API,它在我对照linter进行检查时绝对有效。我使用jQuery来解析它,但我无法弄清楚如何从getJSON返回方法中读取第二个参数。 – Corgalore 2011-04-25 14:39:19

+0

如果你可以提供更多的代码,我可以用我自己的API密钥进行测试 – Jordan 2011-04-26 00:17:29

+0

好的@Martin,我在上面添加了一个代码示例。 – Corgalore 2011-04-26 20:35:35

0

有一个帖子在这里将告诉您如何抓住可能出现的任何错误:$("#msg").ajaxError...

但你可能要使用一个键值对的尝试,是这样的:

$.getJSON(geocoderequest, function(data) { 
    $.each(data, function(key,val) { 
    // etc...