2011-04-04 31 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...