C# 使用 JSON 数组进行 RestSharp 反序列化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16156738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:10:23  来源:igfitidea点击:

RestSharp Deserialization with JSON Array

c#jsonrestsharp

提问by Garrett Vlieger

I have a JSON response that I'm trying to deserialize with RestSharp, and it looks like this:

我有一个 JSON 响应,我试图用RestSharp反序列化它,它看起来像这样:

{"devices":[{"device":{"id":7,"deviceid":"abc123","name":"Name"}},
            {"device":{"id":1,"deviceid":"def456","name":"Name"}}],
 "total":2,
 "start":0,
 "count":2}

Based off of some suggestions I've found, I've tried to setup my POCO like this:

根据我发现的一些建议,我尝试像这样设置我的 POCO:

public class DevicesList
{
    public List<DeviceContainer> Devices;
}

public class DeviceContainer
{
    public Device Device;
}

public class Device
{
    public int Id { get; set; }
    public string DeviceId { get; set; }
    public string Name { get; set; }
}

And then my execution looks like this:

然后我的执行看起来像这样:

// execute the request
var response = client.Execute<DevicesList>(request);

However, response.Datais NULL, and I've tried other variations with no luck.

但是,response.Data是 NULL,我尝试了其他变体但没有运气。

So, what class structure and mapping should be used for this situation? I've also tried this without the extra DeviceContainerclass.

那么,这种情况应该使用什么样的类结构和映射呢?我也试过这个没有额外的DeviceContainer课程。

Thanks for the help.

谢谢您的帮助。

采纳答案by Pete

RestSharp onlyoperates on properties, it does not deserialize to fields, so make sure to convert your Devicesand Devicefields to properties.

RestSharp仅对属性进行操作,它不会反序列化为字段,因此请确保将您的DevicesDevice字段转换为属性。

Also, double check the Content-Typeof the response, if the responses is something non-default, RestSharp may not uses the JsonDeserializer at all. See my answer on RestSharp client returns all properties as null when deserializing JSON response

此外,仔细检查Content-Type响应,如果响应是非默认的,RestSharp 可能根本不使用 JsonDeserializer。反序列化 JSON 响应时,请参阅我在RestSharp 客户端上的回答将所有属性返回为 null

回答by Niels

RestShartp doesn't support DataAnnotation/DataMember, rename your properties with no maj:

RestShartp 不支持 DataAnnotation/DataMember,请重命名您的属性,不要使用主要内容:

  • Devices -> devices
  • Device -> device
  • 设备 -> 设备
  • 设备 -> 设备

AND don't forget the {get; set;};).

并且不要忘记{get; set;};)。

回答by Boycs

I had a slightly different issue when my deserialization POCO contained an array..

当我的反序列化 POCO 包含一个数组时,我遇到了一个稍微不同的问题。

Changing it from Devices[]to List<Devices>resolved the issue and it deserialized correctly.

将其更改Devices[]List<Devices>解决了问题并正确反序列化。

回答by lockwobr

Something that I ran into is, it does not work if your using interfaces like: IEnumerable or IList, it has to be a concrete type.

我遇到的问题是,如果您使用的接口如:IEnumerable 或 IList,则它不起作用,它必须是具体类型。

This will not work, where as it does for some other json serializers like json.net.

这将不起作用,就像 json.net 等其他一些 json 序列化程序一样。

public class DevicesList
{
    public IEnumerable<DeviceContainer> Devices { get; set; }
}

public class DeviceContainer
{
   ...
}

it would have to be something like this:

它必须是这样的:

public class DevicesList
{
    public List<DeviceContainer> Devices { get; set; }
}

public class DeviceContainer
{
   ...
}

回答by Michal Hosala

My problem was entirely different, I naively thought JsonDeserializersupports JsonPropertyattribute, but thats not true. So when trying to deserialize into

我的问题完全不同,我天真地认为JsonDeserializer支持JsonProperty属性,但事实并非如此。所以当试图反序列化为

public class AvailableUserDatasApi
{
    [JsonProperty("available-user-data")]
    public List<AvailableUserDataApi> AvailableUserDatas { get; set; }
}

it failed.. But changing AvailableUserDatasto AvailableUserDatawas enough for things to start working.

它失败了..但更改AvailableUserDatasAvailableUserData足以让事情开始工作。