如何在 Windows Phone 中下载 JSON



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

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

但是在我的 Windows Phone 应用程序中,我无法将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 数据的字符串。我该怎么做?

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

而你的 DownloadStringDone 处理程序是

void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        var container = DeserializeFromJson<DataJsonAttributeContainer>(e.Result); 
    }

最新更新