【问题标题】:How to download JSON in Windows Phone如何在 Windows Phone 中下载 JSON
【发布时间】:2012-10-01 08:40:12
【问题描述】:

我正在尝试在我的 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 数据的字符串。我该怎么做?

【问题讨论】:

    标签: c# json windows-phone-7 webclient json.net


    【解决方案1】:
    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); 
        }
    

    【讨论】:

    • 它下载文件,因为我可以在调试模式下看到 e.Result 中的数据,但容器仍然为空。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多