首页 > 解决方案 > Xamarin C# restsharp 解析嵌套的 json 响应

问题描述

我在解析来自 API 的嵌套 JSON 响应时遇到了一些问题。

API 返回一个数组,其中包含一个对象数组。我面临的问题是数组中对象之前每个数组中包含的 int 值(请参阅附加的示例 JSON 片段)。

我目前已经设置了反序列化类型和对象,这看起来很好,但是,当 restsharp 无法将 INT 值反序列化为我的反序列化类型时,就会出现问题。

杰森

    [
         [
            1064,
            {
                "trx_id": "",
                "block": 0,
                "trx_in_block": 0,
                "op_in_trx": 0,
                "virtual_op": 0,
                "timestamp": "",
                "op": [
                    "vote",
                    {
                        "voter": "user1",
                        "author": "user2",
                        "permlink": "UUID",
                        "weight": 0
                    }
                ]
            }
        ]
    ]

请求

var response = await restClient.Execute<List<List<Models.Responses.AccountHistory.Transaction>>>(request);

事务.CS

    public class Transaction
        {
            public string trx_id { get; set; }
            public long block { get; set; }
            public long trx_in_block { get; set; }
            public long op_in_trx { get; set; }
            public long virtual_op { get; set; }
            //Etc..
    }

我正在尝试反序列化嵌套的事务数组,int 值不是我有任何实际用途的东西。我正在寻找一种忽略整数并仅解析对象的方法。

错误

未处理的异常:Newtonsoft.Json.JsonSerializationException:将值 0 转换为类型“Models.Responses.AccountHistory.Transaction”时出错。路径“[0][0]”,第 1 行,位置 3。发生

标签: c#jsonrestsharp

解决方案


您可以为客户端编写自己的自定义反序列RestSharp化程序。

然后需要在客户端实例化时对其进行设置。


推荐阅读