使用RestSharp反序列化嵌套的JSON数组

时间:2015-08-31 08:27:05

标签: c# restsharp json-deserialization

拥有以下json数组:

[{
"Name": "Component1",
"Count": 2,
"Bulletins": [{
    "ReferenceNumber": "00000A57",
    "Title": "Test test test",
    "PublicationDate": "2014-07-02",
    "List": ["00000A57"]
},
{
    "ReferenceNumber": "10V240000",
    "Title": "Bla bla bla",
    "PublicationDate": "2010-06-04",
    "List": ["10V240000"]
}]
},
{
"Name": "Component2",
"Count": 2,
"Bulletins": [{
    "ReferenceNumber": "00-00-0A-57",
    "Title": "INFORMATION REGARDING BLA BLA",
    "PublicationDate": "2015-05-22",
    "List": ["15-00-89-004",
    "15-00-89-004A"]
},
{
    "ReferenceNumber": "01-02-0B-57",
    "Title": "UNSCHEDULED SUPPLEMENTAL SERVICES",
    "PublicationDate": "2012-09-28",
    "List": ["04-06-01-029",
    "04-26-51-029",
    "04-26-51-029",
    "04-26-51-029",
    "04-26-51-029",
    "04-26-51-029",
    "04-26-51-029"]
}]
}]

我使用以下代码检索NameCount值:

public class BulletinsItemsName
{
    public string Name { get; set; }
    public string Count { get; set; }
    public List<BulletinsContainer> blt { get; set; }
}

public class BulletinsContainer
{
    public Bulletins Bulletins;
}

public class Bulletins
{
    public string ReferenceNumber { get; set; }
    public string Title { get; set; }
    public string PublicationDate { get; set; }
    public string SupersededList { get; set; }
}

要运行我的请求:

var req = request.Execute<List<BulletinsItemsName>>(parameters);

并列出值:

foreach(var xx in req.Data)
{
    Console.WriteLine(xx.Name); 
    foreach(var yz in xx.blt) // Object reference not set to an instance of an object
     {
         Console.WriteLine(yz.Bulletins.Title);
     }
}

如何获取以下内容的值:ReferenceNumberTitlePublicationDateListNameCount都会正确返回所有值,但当我想获得Bulletins值时,会抛出以下错误:Object reference not set to an instance of an object

1 个答案:

答案 0 :(得分:0)

尝试使用公告列表而不是BulletinsContainer - 这是我的清晨,但我不明白为什么你需要一个BulletinsContainer

public List<Bulletins> blt { get; set; }

如果可能,我还建议您以单数形式命名您的课程。