csharp RestSharp将JSON反序列化为动态

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp RestSharp将JSON反序列化为动态相关的知识,希望对你有一定的参考价值。

// 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;

// Remember properties are actually Json.NET JObjects so you have to call .Value to retrieve their string contents.
string firstName = user.FirstName.Value;
string lastName = user.LastName.Value;
// 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);
        }
    }
}

以上是关于csharp RestSharp将JSON反序列化为动态的主要内容,如果未能解决你的问题,请参考以下文章

使用 restsharp 反序列化 json 字符串

unity Restsharp Api Json 序列化和反序列化

RestSharp 获取 REST 数据但不会反序列化

RestSharp用法小结

csharp 使用Newtonsoft JSON.NET将任何对象序列化/反序列化为JSON

RestSharp:从结果中获取空值。调试结果时的数据虽然返回了 JSON