【问题标题】:NullReferenceException while parsing JSON解析 JSON 时出现 NullReferenceException
【发布时间】:2013-01-08 06:00:19
【问题描述】:

当有人单击按钮时,我正在尝试解析 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。

station1station2用在Button2_TapButton3_Tap中,上面代码中没有显示,和上面的Button1_Tap类似。

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

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

【问题讨论】:

    标签: json windows-phone-7 parsing windows-phone windows-phone-8


    【解决方案1】:

    查看这篇博文了解如何Parsing JSON in a Windows Phone Application

    因此,试试这个

    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"];
        }
    

    【讨论】:

    • 太棒了,做到了。非常感谢。
    【解决方案2】:

    由于 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] 属性(请参阅the example for DataContractJsonSerializer),但我可能是错的 :-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-23
      • 2018-04-25
      • 2018-08-11
      • 2018-08-24
      • 2016-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多