![](/img/trans.png)
[英]Why does this nested object initializer throw a null reference exception?
[英]Null exception on nested JSON object
我正在使用Newtonsoft反序列化JSON對象數組。 除了完全嵌套的@attributes對象之外,我能夠成功反序列化整個對象。
這是我要解析的項目的摘要:
"item" : [{
"title" : "Bloody.Birthday.1981.1080p.BluRay.X264-7SinS",
"guid" : "https:\/\/api.nzb.su\/details\/35e799ce66c3290db629b68e3bac20f9",
"link" : "https://",
"comments" : "https://url",
"pubDate" : "Wed, 25 Jun 2014 12:47:16 -0400",
"category" : "Movies > HD",
"description" : "Bloody.Birthday.1981.1080p.BluRay.X264-7SinS",
"enclosure" : {
"@attributes" : {
"url" : "https://url",
"length" : "6777211483",
"type" : "application\/x-nzb"
}
},
"attr" : [{
"@attributes" : {
"name" : "category",
"value" : "2000"
}
}, {
"@attributes" : {
"name" : "category",
"value" : "2040"
}
}, {
"@attributes" : {
"name" : "size",
"value" : "6777211483"
}
}
]
}
]
這是我的課程,用於深入研究和填充項目:
public class item
{
public string title { get; set; }
public string pubDate { get; set; }
public IList<Attr> attr { get; set; }
}
public class Attr
{
public Attributes attribs { get; set; }
}
public class Attributes
{
public string name { get; set; }
public string value { get; set; }
}
我什么時候可以對item.attr計數,但是我得到了正確的#號,但是,如果我嘗試實際獲得列表中屬性的名稱/值,它將給我一個空錯誤。 我做了很多研究,無法確定為什么要在列表中創建適當的項目,但不能確定項目中的值。
有什么想法嗎?
由於@attributes
在C#中不是有效的屬性名稱,因此您需要使用[JsonProperty]
屬性將其映射到JSON,例如:
public class Attr
{
[JsonProperty("@attributes")]
public Attributes attribs { get; set; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.