Restsharp:使用比某些类少/多的字段反序列化json对象 [英] Restsharp: Deserialize json object with less/more fields than some class

查看:134
本文介绍了Restsharp:使用比某些类少/多的字段反序列化json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Restsharp对一些Web服务响应进行反序列化,但是问题是,有时此Web服务会向回发送带有更多字段的json响应.到目前为止,我已经设法通过将所有可能的字段添加到我的匹配模型中来解决这个问题,但是此Web服务将不断在响应中添加/删除字段.

I'm using Restsharp to deserialize some webservice responses, however, the problem is that sometimes this webservices sends back a json response with a few more fields. I've manage to come around this so far by adding all possible field to my matching model, but this web service will keep adding/removing fields from its response.

例如:

有效的Json响应:

{
    "name": "Daniel",
    "age": 25
}

匹配模型:

public class Person
{
    public string name { get; set; }
    public int age { get; set; }
}

这很好用:Person person = deserializer.Deserialize<Person>(response);

现在假设json响应是:

Now suppose the json response was:

{
        "name": "Daniel",
        "age": 25,
        "birthdate": "11/10/1988"
}

是否看到新字段 bithdate ?现在一切都出错了.有没有办法告诉restsharp忽略那些不在模型中的字段?

See the new field bithdate? Now everything goes wrong. Is there a way to tell to restsharp to ignore those fields that are not in the model?

推荐答案

如果您要返回的字段有太多差异,也许最好的方法是跳过静态DTO并反序列化为dynamic.此要点提供了一个示例,说明了如何通过创建自定义解串器来使用RestSharp:

If there's that much variation in the fields you're getting back, perhaps the best approach is to skip the static DTOs and deserialize to a dynamic. This gist provides an example of how to do this with RestSharp by creating a custom deserializer:

// ReSharper disable CheckNamespace
namespace RestSharp.Deserializers
// ReSharper restore CheckNamespace
{
    public class DynamicJsonDeserializer : IDeserializer
    {
        public string RootElement { get; set; }
        public string Namespace { get; set; }
        public string DateFormat { get; set; }

        public T Deserialize<T>(RestResponse response) where T : new()
        {
            return JsonConvert.DeserializeObject<dynamic>(response.Content);
        }
    }
}

用法:

// Override default RestSharp JSON deserializer
client = new RestClient();
client.AddHandler("application/json", new DynamicJsonDeserializer());

var response = client.Execute<dynamic>(new RestRequest("http://dummy/users/42"));

// Data returned as dynamic object!
dynamic user = response.Data.User;

一个更简单的选择是使用 Flurl.Http (免责声明:我是作者),一个HTTP客户端库,在未提供通用参数的情况下,默认情况下会反序列化为dynamic:

A simpler alternative is to use Flurl.Http (disclaimer: I'm the author), an HTTP client lib that deserializes to dynamic by default when generic arguments are not provided:

dynamic d = await "http://api.foo.com".GetJsonAsync();

在这两种情况下,实际的反序列化都是由 Json.NET 执行的.使用RestSharp,您需要将包添加到项目中(尽管很有可能已经拥有了); Flurl.Http对此有依赖性.

In both cases, the actual deserialization is performed by Json.NET. With RestSharp you'll need to add the package to your project (though there's a good chance you have it already); Flurl.Http has a dependency on it.

这篇关于Restsharp:使用比某些类少/多的字段反序列化json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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