我正在使用JSON.Net反序列化JSON字符串。 JSON字符串是JSON.Net的反序列化返回'null'
string testJson = @"{
""Fruits"": {
""Apple"": {
""color"": ""red"",
""size"": ""round""
},
""Orange"": {
""Properties"": {
""color"": ""red"",
""size"": ""round""
}
}
}
}";
和我的代碼是
public class Fruits
{
public Apple apple { get; set; }
public Orange orange { get; set; }
}
public class Apple
{
public string color { get; set; }
public string size { get; set; }
}
public class Orange
{
public Properties properties { get; set; }
}
public class Properties
{
public string color { get; set; }
public string size { get; set; }
}
我想用下面的代碼
Fruits result = JsonConvert.DeserializeObject<Fruits>(testJson);
我在結果的問題,反序列化這個是水果與蘋果和橙色有null
而不是他們的屬性,顏色,尺寸。
問題是最外層的'{...}'護腕對應着你的'Fruits'類型,'Fruits'不包含名爲'Fruits'的屬性。嘗試創建一個包含'Fruits'屬性的容器類型,並對其進行反序列化。 – 2014-11-25 13:54:28
或者按照其他方式將某些內容序列化爲JSON以瞭解正確的格式。 – DavidG 2014-11-25 13:58:15