我试图在 C# 中反序列化 JSON,但我得到 NullReferenceException,我不知道为什么。
这是我要解析的 JSON:
{"Entries": {"Entry": {"day": "28","month": "10","year": "1955","type": "birthday","title": "Bill Gates was born!","picture": "","video": ""}}}
我正在使用这段代码
public class Entry
{
public string day { get; set; }
public string month { get; set; }
public string year { get; set; }
public string type { get; set; }
public string title { get; set; }
public string picture { get; set; }
public string video { get; set; }
}
public class Entries
{
public List<Entry> entry { get; set; }
}
private void buttonSearch_Click(object sender, EventArgs e)
{
string json = new StreamReader("events.json").ReadToEnd();
var entries = JsonConvert.DeserializeObject<Entries>(json);
MessageBox.Show(entries.entry[0].day); // NullReferenceException
}
为什么我会收到此错误,我该如何解决?
当我将 JSON 更改为
{"Entries": ["Entry": {"day": "28","month": "10","year": "1955","type": "birthday","title": "Bill Gates was born!","picture": "","video": ""}]}
我明白了After parsing a value an unexpected character was encountered: :. Path 'Entries[0]', line 1, position 20.
编辑
我玩过 JSON,下面的那个对我有用:
[{"day": "28","month": "10","year": "1955","type": "birthday","title": "Bill Gates was born!","picture": "","video": ""}]