使用 RestSharp 反序列化嵌套的 JSON 数组 [英] Deserializing nested JSON arrays using RestSharp

查看:96
本文介绍了使用 RestSharp 反序列化嵌套的 JSON 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 RestSharp 反序列化以下 JSON 响应.我尝试了各种模型结构来提取数据无济于事.我一直被嵌套数组绊倒.

我无法控制服务,因此无法更改格式.

JSON 格式:

<预><代码>[{结果":19,状态码":200,},[{身份证":24,姓名":鲍勃"},{身份证":82,姓名":爱丽丝"}]]

使用此模型,我已经能够从第一个对象中提取数据,但仅此而已.我不确定如何准确读取对象后面的数组.

公共类信息{公共 int 结果 { 得到;放;}公共 int 状态码 { 获取;放;}}

去盐化示例:

var deserializer = new JsonDeserializer();var wat = deserializer.Deserialize<List<List<Info>>>(响应);

我是在这里完全遗漏了一些东西,还是我唯一的选择来编写自定义解串器和/或使用类似 JSON.NET 的东西?

解决方案

问题是你的 JSON 数组异常多态:它的第一个元素是一个对象,它的第二个元素是一个对象数组.一种更自然的表示方法是将其作为具有两个命名属性的 JSON 对象——但这不是您所得到的.使用任何序列化程序将其直接反序列化为具有两个命名属性的 c# POCO 在一个步骤中将会很棘手,因为 JSON 数据模型与您想要的数据模型完全不同.相反,反序列化为中间表示并进行转换可能是最简单的.幸运的是 RestSharp 有合适的中间类 JsonObjectJsonArray.

因此,如果您想反序列化为以下类:

公共类信息{公共 int 结果 { 得到;放;}公共 int 状态码 { 获取;放;}}公共类 IdAndName{公共 int id { 获取;放;}公共字符串名称 { 获取;放;}}公共类响应内容{公共信息信息{获取;放;}公共列表数据{得到;放;}}

你可以这样做:

 var array = (JsonArray)SimpleJson.DeserializeObject(response.Content);var responseContent = (array == null ? (ResponseContent)null : new ResponseContent(){Info = array.OfType().Select(o => SimpleJson.DeserializeObject(o.ToString())).FirstOrDefault(),Data = array.OfType().SelectMany(a => SimpleJson.DeserializeObject>(a.ToString())).ToList()});

I'm trying to deserialize the following JSON response using RestSharp. I have tried various model structures to extract the data to no avail. I keep getting tripped up on the nested arrays.

I do not have control over the service, so I can't change the format.

JSON Format:

[
    {
        "results": 19,
        "statuscode": 200,
    },
    [
        {
            "id": 24,
            "name": "bob"
        },
        {
            "id": 82,
            "name": "alice"
        }
    ]
]

Using this model, I've been able to pull the data from the first object, but that's all. I'm not sure how exactly to read through the array that comes after the object.

public class Info
{
    public int results { get; set; }
    public int statuscode { get; set; }
}

Example deseralization:

var deserializer = new JsonDeserializer();
var wat = deserializer.Deserialize<List<List<Info>>>(response);

Am I just completely missing something here or are my only options to write a custom deserializer and/or use something like JSON.NET?

解决方案

The problem is that your JSON array is exceptionally polymorphic: its first element is an object, and its second element is an array of objects. A more natural way to represent this would have been as a JSON object with two named properties -- but that's not what you have been given. Deserializing this directly to a c# POCO with two named properties in a single step with any serializer is going to be tricky since the JSON data model is quite different than your desired data model. Instead, it may be easiest to deserialize to an intermediate representation and convert. Luckily RestSharp has appropriate intermediate classes JsonObject and JsonArray.

Thus, if you want to deserialize to the following classes:

public class Info
{
    public int results { get; set; }
    public int statuscode { get; set; }
}

public class IdAndName
{
    public int id { get; set; }
    public string name { get; set; }
}

public class ResponseContent
{
    public Info Info { get; set; }
    public List<IdAndName> Data { get; set; }
}

You can do:

        var array = (JsonArray)SimpleJson.DeserializeObject(response.Content);
        var responseContent = (array == null ? (ResponseContent)null : new ResponseContent()
        {
            Info = array.OfType<JsonObject>().Select(o => SimpleJson.DeserializeObject<Info>(o.ToString())).FirstOrDefault(),
            Data = array.OfType<JsonArray>().SelectMany(a => SimpleJson.DeserializeObject<List<IdAndName>>(a.ToString())).ToList()
        });

这篇关于使用 RestSharp 反序列化嵌套的 JSON 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆