2012-09-15 56 views
4

我正在製作一個windows phone應用程序,它可以從此鏈接使用json http://dir.yummly.com/4/public/8a753a70358c827b01358da787bd68f3,但是當我嘗試異步下載數據時,我無法以可讀語言獲取e.result。 這裏是我的C#代碼 -在Windows Phone 7中使用json webservice

Uri sub4 = new Uri("http://dir.yummly.com/4/public/8a753a70358c827b01358da787bd68f3"); 
     string kit = sub4.ToString(); 
    WebClient w = new WebClient(); 
     w.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted); 
     w.DownloadStringAsync(sub4); 
     MessageBox.Show("done"); 

     } 
    void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
    { 
    string fres = e.Result;// here e.result was � 
    } 

然後,我想反序列化e.result。 我的代碼適用於其他任何json網址。而且,因爲我正在開發Windows Phone應用程序,所以我無法使用其他webcilent方法,例如DownloadData和DownloadString。我也看到了utf8和unicode之間的很多轉換,但沒有一個能夠工作。任何人都可以請幫忙?

回答

2

您正在碰到的API提供gzip內容,這就是您所看到的垃圾文本。在使用之前,您需要解壓縮內容。您可以從標題告訴這:

Content-Encoding: gzip 

不幸的是,我最後一次檢查,有在Windows Phone中gzip壓縮的響應支持沒有內置。創建解決方法不應太難。當使用谷歌搜索時,結果表明影響最小的解決方案是從NuGet中添加SharpGIS.GZipWebClient庫並使用GZip支持。

+1

非常感謝你,先生。你很棒..!!對於那些有同樣問題的人可以參考這個鏈接 - http://dotnetbyexample.blogspot.in/2012/01/json-deserialization-with-jsonnet.html – user1673567