问题描述
我有一个JSON响应,我试图用反序列化,它看起来是这样的:
I have a JSON response that I'm trying to deserialize with RestSharp, and it looks like this:
{"devices":[{"device":{"id":7,"deviceid":"abc123","name":"Name"}},
{"device":{"id":1,"deviceid":"def456","name":"Name"}}],
"total":2,
"start":0,
"count":2}
根据客一些建议,我发现,我已经尝试设置我的POCO是这样的:
Based off of some suggestions I've found, I've tried to setup my POCO like this:
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);
然而, response.Data code>为NULL,我已经试过,没有运气其他变化。
However, response.Data
is NULL, and I've tried other variations with no luck.
那么,应该用什么类结构和映射这种情况呢?我也试过这种没有额外的 DeviceContainer
类。
So, what class structure and mapping should be used for this situation? I've also tried this without the extra DeviceContainer
class.
感谢您的帮助。
推荐答案
RestSharp的只有的操作上的属性,它不反序列化到田地,所以一定要转换您的设备
和设备
来属性的字段。
RestSharp only operates on properties, it does not deserialize to fields, so make sure to convert your Devices
and Device
fields to properties.
此外,仔细检查响应的内容类型
,如果反应是什么非默认,RestSharp可能无法使用JsonDeserializer的。请参见
Also, double check the Content-Type
of 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
这篇关于RestSharp反序列化使用JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!