How does JSON conversion in. NET only handle partial attribute two?

Source: Internet
Author: User



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" &GT;&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" &GT;&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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.