解析JSON时出现NullReferenceException
当有人单击按钮时,我试图解析一个JSON文件,它用JSON中的数据替换按钮的内容 目前我面临一个数据仍然为空的问题。代码如下:解析JSON时出现NullReferenceException,json,windows-phone-7,parsing,windows-phone,windows-phone-8,Json,Windows Phone 7,Parsing,Windows Phone,Windows Phone 8,当有人单击按钮时,我试图解析一个JSON文件,它用JSON中的数据替换按钮的内容 目前我面临一个数据仍然为空的问题。代码如下: private void Button1_Tap(object sender, System.Windows.Input.GestureEventArgs e) { Button1.FontSize = 15; Button1.Content = "Fetching..."; var client = new We
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; }
}
root
和songHistory
保持为null,因此引发NullReferenceException
station1
和station2
用于Button2\u-Tap
和Button3\u-Tap
,未在上述代码中显示,与上述Button1\u-Tap
类似
我被告知DataContractJsonSerializer无法将json对象中的属性“1”与RadioRootObject类上的属性station1匹配,但我不确定如何使其匹配
我无法更改JSON本身中的数据。有什么想法吗?因为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]属性(请参阅),但我可能错了:-)因为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]属性(请参阅),但我可能错了:-)查看这篇博客文章,了解如何 因此,试试这个
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"];
}
查看此博客文章了解如何 因此,试试这个
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"];
}
太棒了,成功了。非常感谢。太棒了,成功了。非常感谢。