简体   繁体   English

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

[英]Deserialize nested JSON array using RestSharp

Having the following json array: 具有以下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"]
}]
}]

I'm using the following code to retrieve the Name and Count values: 我正在使用以下代码来检索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; }
}

To run the request i have: 要运行请求,我有:

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

And to list values: 并列出值:

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);
     }
}

How can I get values for: ReferenceNumber , Title , PublicationDate and List ? 如何获取以下值: ReferenceNumberTitlePublicationDateList All values are correctly returned for Name and Count but when I want to get Bulletins values the following error is thrown: Object reference not set to an instance of an object . 所有值都正确返回了NameCount但是当我想获取Bulletins值时,将引发以下错误: Object reference not set to an instance of an object

Try using a list of Bulletins instead of BulletinsContainer - It's early morning for me, but I can't see why you need a BulletinsContainer 尝试使用Bulletins列表而不是BulletinsContainer-对我来说这是清晨,但是我不明白为什么您需要BulletinsContainer

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

I would also recommend, if possible, that you name your classes in their singular form. 如果可能,我还建议您以单数形式命名类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM