c# - 使用 JSON 数组的 RestSharp 反序列化

标签 c# json restsharp

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

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

根据我发现的一些建议,我尝试像这样设置我的 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; }
}

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

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

但是,response.Data 为 NULL,我尝试了其他变体但没有成功。

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

感谢您的帮助。

最佳答案

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

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

关于c# - 使用 JSON 数组的 RestSharp 反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16156738/

相关文章:

C# 表达式使用 == 或等于运算符和外部参数轻松转换

c# - 使用 NPOI 将 Excel 单元格区域格式化为表格

java - 在 Jersey 中使用 JAXB 解析 JSON

android - 如何将带小数位的 DOUBLE 放入 JSONObject?

python - 无法在自定义编码器中将字典编码为 JSON

c# - 是否可以使用 servicestack 上传文件和发布数据?

c# - 有没有办法将 restsharp 与 Windows 10 通用应用程序一起使用?

c# - WPF 使列滑入位置 XAML

c# - 使用 IEnumerable 检测修改

c# - RestSharp 下载数据与进度条同步