简体   繁体   English

RestSharp JSON 数组反序列化

[英]RestSharp JSON Array deserialization

I launch this RestSharp query in JSON format:我以 JSON 格式启动这个 RestSharp 查询:

var response = restClient.Execute<Report>(request);

The response I get contains this data我得到的响应包含这些数据

[
    {
        "Columns":
        [
            {"Name":"CameraGuid","Type":"Guid"},
            {"Name":"ArchiveSourceGuid","Type":"Guid"},
            {"Name":"StartTime","Type":"DateTime"},
            {"Name":"EndTime","Type":"DateTime"},
            {"Name":"TimeZone","Type":"String"},
            {"Name":"Capabilities","Type":"UInt32"}
        ],
        "Rows":
        [
            [
                "00000001-0000-babe-0000-00408c71be50",
                "3782fe37-6748-4d36-b258-49ed6a79cd6d",
                "2013-11-27T17:52:00Z",
                "2013-11-27T18:20:55.063Z",
                "Eastern Standard Time",
                2147483647
            ]
        ]
    }
]

I'm trying to deserialize it into this group of classes:我正在尝试将其反序列化为这组类:

public class Report
{
    public List<ReportResult> Results { get; set; }
}

public class ReportResult
{
    public List<ColumnField> Columns { get; set; }
    public List<RowResult>   Rows { get; set; }
}

public class ColumnField
{
    public string Name { get; set; }
    public string Type { get; set; }
}

public class RowResult
{
    public List<string> Elements { get; set; }
}

Unfortunately, the result data is null and I get this exception:不幸的是,结果数据为空,我得到这个异常:

Unable to cast object of type 'RestSharp.JsonArray' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'.无法将“RestSharp.JsonArray”类型的对象转换为“System.Collections.Generic.IDictionary`2[System.String,System.Object]”类型。

I cannot figure out what is wrong here.我无法弄清楚这里出了什么问题。 I little help would be greatly appreciated.我一点帮助将不胜感激。

Try this:试试这个:

var response = restClient.Execute<List<ReportResult>>(request);

EDIT编辑

You should also change ReportResult to:您还应该将ReportResult更改为:

public class ReportResult
{
  public List<ColumnField> Columns { get; set; }
  public List<List<string>>   Rows { get; set; }
}

and you can get rid of Report and RowResult .你可以摆脱ReportRowResult

There is another way by creating wrapper class:通过创建包装类还有另一种方法:

public class ThirdPartySuggesters : List<ThirdPartySuggester> {}
var response = client.Execute<ThirdPartySuggesters>(request);

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

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