0

当有人单击按钮时,我正在尝试解析 JSON 文件,该按钮将按钮的内容替换为来自 JSON 的数据。

目前我面临一个数据保持为空的问题。代码如下:

private void Button1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        Button1.FontSize = 15;
        Button1.Content = "Fetching...";
        var client = new WebClient();
        client.OpenReadCompleted +=
            (s, eargs) =>
            {
                var serializer = new DataContractJsonSerializer(typeof(RadioRootObject));
                if (eargs.Error != null)
                {
                    if (eargs.Error.Message.Contains("NotFound"))
                    {
                        MessageBox.Show("Could not retrieve playlist", "Error", MessageBoxButton.OK);
                        Button1.Content = "Could not retrieve playlist";
                    }
                    else
                    {
                        MessageBox.Show("Could not retrieve playlist", "Error", MessageBoxButton.OK);
                        Button1.Content = "Could not retrieve playlist";
                    }
                }
                else
                {
                    var root = (RadioRootObject)serializer.ReadObject(eargs.Result);
                    var songHistory = root.station3;
                    Button1.Content = songHistory.text;
                }
            };
        var uri = new Uri("http://www.reignofcomputer.com/tmpsend/nowplaying.json");
        client.OpenReadAsync(uri);
    }

    public class station1
    {
        public string station { get; set; }
        public string title { get; set; }
        public string artist { get; set; }
        public string text { get; set; }
    }

    public class station2
    {
        public string station { get; set; }
        public int listeners { get; set; }
        public string title { get; set; }
        public string artist { get; set; }
        public string text { get; set; }
    }

    public class station3
    {
        public string station { get; set; }
        public int listeners { get; set; }
        public string title { get; set; }
        public string artist { get; set; }
        public string text { get; set; }
    }

    public class RadioRootObject
    {
        public station1 station1 { get; set; }
        public station2 station2 { get; set; }
        public station3 station3 { get; set; }
    }

rootsongHistory保持为空,因此引发 NullReferenceException。

station1andstation2用在Button2_TapandButton3_Tap中,上面代码中没有显示,与上面类似Button1_Tap

我被告知 DataContractJsonSerializer 无法将 json 对象中的属性“1”与 RadioRootObject 类上的属性 station1 匹配,但我不确定如何让它匹配。

我无法更改 JSON 本身的数据。有任何想法吗?

4

2 回答 2

2

查看这篇博文,了解如何在 Windows Phone 应用程序中解析 JSON

因此,试试这个

private void Button_Click(object sender, RoutedEventArgs e)
    {
        Button1.FontSize = 15;
        Button1.Content = "Fetching...";var client = new WebClient();
        var uri = new Uri("http://www.reignofcomputer.com/tmpsend/nowplaying.json");
        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
        client.DownloadStringAsync(uri);
    }

void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        var jobj = JObject.Parse(e.Result);
        var station3 = jobj["3"];
        Button1.Content = station3["text"];
    }
于 2013-01-08T08:28:12.780 回答
1

由于 JSON 中的属性为“1”,而 RadioRootObject 的成员名称为“station1”,因此它们不匹配。您可以使用 DataMemberAttribute 告诉序列化程序 JSON 中的名称是什么,例如

public class RadioRootObject
{
    [DataMember(Name="1")]
    public station1 station1 { get; set; }
    [DataMember(Name="2")]
    public station2 station2 { get; set; }
    [DataMember(Name="3")]
    public station3 station3 { get; set; }
}

老实说,我认为无论如何你的类和成员都必须具有 [DataContract] 和 [DataMember] 属性(参见DataContractJsonSerializer 的示例),但我可能是错的 :-)

于 2013-01-08T07:35:35.740 回答