2012-09-01 43 views
1
public async void MakeRequest(string requestUrl) 
    { 
     try 
     { 
      HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest; 
      using (HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse) 
      { 
       if (response.StatusCode != HttpStatusCode.OK) 
        throw new Exception(string.Format(
        "Server error(HTTP {0}:{1}.", 
        response.StatusCode, 
        response.StatusDescription)); 
       DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(RootObject)); 
       object objResponse = jsonSerializer.ReadObject(response.GetResponseStream()); 
       root = (RootObject)objResponse; 

       if (root == null) 
        this.Frame.Navigate(typeof(MainPage)); 
      } 
     } 
     catch (Exception ex) 
     { 
      ThrowException(ex); 
     } 
    } 

鏈接:http://dev.virtualearth.net/REST/V1/Routes?wp.0=39.920829,32.853883&wp.1=39.877666,32.864728&key=BingMapsKeyREST服務 - 反序列化JSON錯誤:遇到意外的字符 '<'

有什麼錯誤的,我在上面的代碼?

回答

0

這段代碼有一次我跟在Bing Maps REST Service .NET Libraries發現BingMapsRESTService.Common.JSON.Response類型@rbrundritt建議您使用in your other post

+0

沒有改變。 –

+0

嗯...我剪切和粘貼你的代碼就像它,並拋出一個按鈕後面。我已將我的VS2012項目放在DropBox上,位於https://www.dropbox.com/s/hoiszqnhwivuim2/jsondeser.zip - 放入Bing密鑰,看看會發生什麼。該應用程序沒有做任何事情,但是您可以在ReadObject的返回中放置一個斷點以查看數據。 –

5

只要改變這條線讓使用JSON查詢替換未定義RootObject在你的代碼工作正常,我和不是XML格式

所以從這個...

string query = "http://dev.virtualearth.net/REST/v1/Locations/" +   
       queryString + "?output=xml" + " &key=" + BingMapsKey; 

要這個......

string query = "http://dev.virtualearth.net/REST/v1/Locations/" + 
       queryString + "?output=json" + " &key=" + BingMapsKey; 

即只改變output=xmloutput=json