C#学习教程:如何使用Json.Net序列化/反序列化带有其他属性的自定义集合它有一些像这样的自定义属性:classFooCollection:IList{privateList_foos=newList();公共字符串栏{得到;放;}//实现IList、ICollection和IEnumerable成员...当我序列化时,我使用以下代码:JsonSerializerSettingsjss=newJsonSerializerSettings(){TypeNameHandling=TypeNameHandling.Auto};字符串serializedCollection=JsonConvert.SerializeObject(值,jss);它序列化和反序列化所有正确的集合项;但是,不考虑FooCollection类中的任何额外属性。无论如何将它们包含在序列化中?问题如下:当一个对象实现了IEnumerable时,JSON.net将其识别为值数组,并按照数组Json语法(不包括属性)进行序列化,例如:[{"FooProperty":123},{"FooProperty":456},{"FooProperty":789}]如果你想保留属性的序列化,你需要通过定义一个自定义的JsonConverter来手动处理这个对象的序列化://可以序列化的中间类通过JSON.net//并包含与FooCollection相同的数据classFooCollectionSurrogate{//foo元素的集合publicListCollection{get;放;}//FooCollection的属性序列化publicstringBar{get;放;}}publicclassFooCollectionConverter:JsonConverter{publicoverrideboolCanConvert(TypeobjectType){returnobjectType==typeof(FooCollection);}publicoverrideobjectReadJson(JsonReaderreader,TypeobjectType,objectexistingValue,JsonSerializerserializer){//NBnull处理缺失varserializerDe=serializer(.reader);varfooElements=surrogate.Collection;varfooColl=newFooCollection{Bar=surrogate.Bar};前额ch(fooElements中的变量el)fooColl.Add(el);返回fooColl;}publicoverridevoidWriteJson(JsonWriterwriter,objectvalue,JsonSerializerserializer){//NBnull处理缺失varfooColl=(FooCollection)value;//创建代理并序列化它而不是//集合本身varsurrogate=newFooCollectionSurrogate(){Collection=fooColl.ToList(),Bar=fooColl.Bar};serializer.Serialize(作者,代理人);然后使用如下:varss=JsonConvert.SerializeObject(collection,newFooCollectionConverter());varobj=JsonConvert.DeserializeObject(ss,newFooCollectionConverter());我个人喜欢尽可能避免编写自定义JsonConverter,而是使用为此目的设计的各种JSON属性您可以简单地用JsonObjectAttribute修饰FooCollection,这会强制序列化为JSON对象而不是数组。您必须使用JsonIgnore修饰Count和IsReadOnly属性,以防止它们出现在输出中。如果要将_foos保留为私有字段,还必须使用JsonProperty对其进行修饰。[JsonObject]classFooCollection:IList{[JsonProperty]privateList_foos=newList();公共字符串栏{得到;放;}//IList实现[JsonIgnore]publicintCount{...}[JsonIgnore]publicboolIsReadOnly{...}}序列化产生这样的东西:{"_foos":["foo1","foo2"],"Bar":"bar"}显然,只有当您可以更改FooCollection的定义以添加这些属性时,FooCollection,否则您必须采用自定义转换器的方式。继承列表有效吗?classFooCollection:List,IList{publicstringBar{get;放;}//实现IList、ICollection和IEnumerable成员...}如果您还想保留List或集合本身的内容,可以考虑暴露属性ReturnList。它必须是封装以防止在序列化时出现循环问题:[JsonObject]publicclassFooCollection:List{[DataMember]publicstringBar{get;放;}="酒吧";publicICollectionItems=>new_(this);}publicclass_:ICollection{public_(ICollectioncollection)=>Inner=collection;公共ICollection内部{得到;}publicintCount=>this.Inner.Count;publicboolIsReadOnly=>this.Inner.IsReadOnly;publicvoidAdd(Titem)=>this.Inner.Add(item);publicvoidClear()=>this.Inner.Clear();publicboolContains(Titem)=>this.Inner.Contains(item);publicvoidCopyTo(T[]array,intarrayIndex)=>this.Inner.CopyTo(array,arrayIndex);publicIEnumeratorGetEnumerator()=>this.Inner.GetEnumerator();publicboolRemove(Titem)=>this.Inner.Remove(item);IEnumeratorIEnumerable.GetEnumerator()=>this.Inner.GetEnumerator();}newFooCollection{1,2,3,4,4}=>{"Bar":"Bar","Items":[1,2,3],"Capacity":4,"Count":3}newFooCollection{1,2,3}.ToArray()=>new[]{1,2,3}.ToArray()以上是C#学习教程:HowtouseJson.Nettoserialize/deserializeacustomcollectionwithotherproperties。分享所有内容,如果对大家有用还有我需要了解更多C#学习教程,希望大家多多关注~本文收集自网络,不代表立场,如涉及侵权,请谅解点击右侧联系管理员删除如有转载请注明出处:
