为什么反序列化是JSON.NET工作不继承 [英] Why is JSON.NET is not working with inheritance while deserializing

查看:150
本文介绍了为什么反序列化是JSON.NET工作不继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我反序列化的JSON字符串通过使用正常工作下面的类来根对象。

  [Serializable接口]
    公共类MoviesListRootObject
    {
        公众诠释计数{获得;组; }
        公共分页分页{获得;组; }
        公开名单<响应>响应{获得;组; }
    }
 

.............

  VAR JSON = wc.DownloadString(jsonRequestURL);
VAR rootObj = JsonConvert.DeserializeObject< MoviesListRootObject>(JSON);
 

但是,如果我generalizng根对象BT建立的父类,然后从它继承,然后我得到反序列化后空!!!!

  [Serializable接口]
    公共类RootObject
    {
        公众诠释计数{获得;组; }
        公共分页分页{获得;组; }
    }

[可序列化]
    公共类MoviesListRootObject:RootObject
    {
        公开名单< MovieResponse> movieResponse {获得;组; }

    }
 

..............................................

  VAR JSON = wc.DownloadString(jsonRequestURL);
 VAR rootObj = JsonConvert.DeserializeObject< MoviesListRootObject>(JSON);
 

解决方案

这是很简单,由json.net提供的内置支持,你只需要使用下面的JsonSettings同时序列化和反序列化:

  JsonConvert.SerializeObject(图,Formatting.None,新JsonSerializerSettings()
                                                                                               {
                                                                                                 TypeNameHandling = TypeNameHandling.All,
                                                                                               TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple
                                                                                           });
 

和Deserialzing使用低于code:

  JsonConvert.DeserializeObject(Encoding.UTF8.GetString(BDATA),类型,
                                                       新JsonSerializerSettings()
                                                           {TypeNameHandling = TypeNameHandling.All});
 

就拿记下JsonSerializerSettings的对象初始化,这是对你很重要。

I am deserializing the JSON string to root object by using the following class which works fine .

[Serializable]
    public class MoviesListRootObject
    {
        public int count { get; set; }
        public Pagination pagination { get; set; }
        public List<Response> response { get; set; }
    }

...................................

var json = wc.DownloadString(jsonRequestURL);
var rootObj = JsonConvert.DeserializeObject<MoviesListRootObject>(json);

But if I am generalizng the root object bt creating parent class and then inheriting from it , then I get null after deserialization!!!!

[Serializable]
    public class RootObject
    {
        public int count { get; set; }
        public Pagination pagination { get; set; }
    }

[Serializable]
    public class MoviesListRootObject:RootObject
    {
        public List<MovieResponse> movieResponse { get; set; }

    }

..............................................

 var json = wc.DownloadString(jsonRequestURL);
 var rootObj = JsonConvert.DeserializeObject<MoviesListRootObject>(json);

解决方案

It is quite simple and out of the box support provided by json.net, you just have to use the following JsonSettings while serializing and Deserializing:

JsonConvert.SerializeObject(graph,Formatting.None, new JsonSerializerSettings()
                                                                                               {
                                                                                                 TypeNameHandling = TypeNameHandling.All,
                                                                                               TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple
                                                                                           });

and for Deserialzing use the below code:

JsonConvert.DeserializeObject(Encoding.UTF8.GetString(bData),type,
                                                       new JsonSerializerSettings()
                                                           {TypeNameHandling = TypeNameHandling.All});

Just take a note of the JsonSerializerSettings object initializer, that is important for you.

这篇关于为什么反序列化是JSON.NET工作不继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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