the JSON method used in the first article. The ability to handle only the required properties, but requires that the attribute cannot be a complex type, such as a generic, array. Other user-defined classes, etc., limit too much, so see if they can improve, unexpectedly Newtonsoft.json provide a relevant interface, just need to implement it.
Just inherit the defaultcontractresolver and rewrite a method to do it.
Core code:
<summary>///object to JSON, all properties output//</summary>//<typeparam name= "ObjType" >&L t;/typeparam>//<param name= "obj" ></param>///<returns></returns> Publi C static string objtojsonstring<objtype> (ObjType obj) where Objtype:class {string s = Jsonconve Rt. SerializeObject (obj); return s; }///<summary>//object to JSON, output only partial properties///</summary>//<typeparam name= "T" ; object type </typeparam>//<param name= "T" > Object type value </param>//<param name= "Propertyinfos" > The name of the property that needs to be processed by JSON. Comma delimited </param>////<returns></returns> public static string objtojsonstring<t> (T T, s Tring Propertyinfos) where T:class {string[] cols = Propertyinfos.split (new char[] {', '}, STRINGSP Litoptions.removeemptyentries); List<string> _propertynames = New List<string> (); foreach (String col in cols) { string coltemp = col. ToLower (). Trim (); if (!_propertynames.contains (coltemp)) {_propertynames.add (coltemp); }} string s = Jsonconvert.serializeobject (t, formatting.indented, new Jsonserializersettings {C Ontractresolver = new Dynamiccontractresolver (_propertynames)}); return s; }///<summary>//JSON to object///</summary>//<typeparam name= "ObjType" >&L t;/typeparam>//<param name= "jsonstring" ></param>///<returns></returns> public static ObjType jsonstringtoobj<objtype> (String jsonstring) where Objtype:class { ObjType s = jsonconvert.deserializeobject<objtype> (jsonstring); return s; } class DynamiccontraCtresolver:defaultcontractresolver {private readonly list<string> _propertynames; Public Dynamiccontractresolver (list<string> propertynames) {_propertynames = PropertyName S }///<summary>///Assume that the property is an object. It is necessary to pass the property of this object to propertynames, or/or otherwise, when processing this object property, it does not include the corresponding attribute value///</summary>//<param Name= "type" ></param>//<param name= "memberserialization" ></param>//<re turns></returns> protected override ilist<jsonproperty> createproperties (type type, memberserial ization memberserialization) {ilist<jsonproperty> properties = base. Createproperties (type, memberserialization); ilist<jsonproperty> Propertiesreturn = new list<jsonproperty> ();//properties that require JSON processing foreach (Jsonprop Erty item in properties) { string propertynametemp = Item. Propertyname.tolower (). Trim (); if (_propertynames.contains (propertynametemp)) {Propertiesreturn.add (item); }} return Propertiesreturn; } }
How does JSON conversion in. NET only handle partial attribute two?