1

我正在尝试在我的 Windows Phone 应用程序中下载 JSON,然后对其进行解析。通过互联网搜索,我发现上面的代码应该可以正常工作:

using (WebClient wc = new WebClient())
{
    string result = wc.DownloadString("http://data.nature.com/sparql");
}

但在我的 Windows 手机应用程序中,我无法分配wc.DownloadStringAsync()给字符串类型变量。

我的代码:

WebClient webClient = new WebClient();
        webClient.DownloadStringAsync(new Uri("http://184.22.234.221/bfunction/mjson.php"));
        var container = DeserializeFromJson<DataJsonAttributeContainer>(JsonStr); 

在这里,JsonStr 是我要分配下载的 JSON 数据的字符串。我怎样才能做到这一点?

4

1 回答 1

2
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new Uri("http://184.22.234.221/bfunction/mjson.php"));

你的 DownloadStringCompleted 处理程序是

void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        var container = DeserializeFromJson<DataJsonAttributeContainer>(e.Result); 
    }
于 2012-10-01T09:30:57.203 回答