about what I do with JSON in my work.
First: Dynamic serialization classes
Second: Time format processing
Usually we have 10 or more properties in a class, but we usually only need to serialize three to five of them so there will be extra data.
If I just want to serialize ID with name how to handle
This is the method I am looking for online:
Using newtonsoft.json;using newtonsoft.json.converters;using newtonsoft.json.serialization;using System;using System.collections.generic;using system.linq;namespace CYP. new.wcf.common.common{public class Limitpropscontractresolver:defaultcontractresolver { private string[] props = null; Public Limitpropscontractresolver (string[] props) { this.props = props; } protected override ilist<jsonproperty> Createproperties (type type, memberserialization memberserialization) { ilist<jsonproperty> list = base. Createproperties (type, memberserialization); Only the list of the listed return list is reserved. Where (p = = props. Contains (P.propertyname)). ToList ();}}}
--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- ----------------------------------------
About json.net processing date format look at the first picture. Json.NET Processing Date format This is not what normal human beings can see.
You might find that json.net has something to do with the date.
But this time you'll find that the json.net of the heavy load inside
public static string SerializeObject (object value); public static string SerializeObject (object value, formatting formatting); public static string SerializeObject (object value, jsonserializersettings settings); public static string SerializeObject (object value, params jsonconverter[] converters); public static string SerializeObject (object value, formatting formatting, jsonserializersettings settings); public static string SerializeObject (object value, formatting formatting, params jsonconverter[] converters);
You will find that the dynamic serialization and the format of the time processing cannot coexist this problem really makes my little egg ache a bit ....
Solution:
Using newtonsoft.json;using newtonsoft.json.converters;using newtonsoft.json.serialization;using System;using System.collections.generic;using system.linq;namespace CYP. new.wcf.common.common{public class Limitpropscontractresolver:defaultcontractresolver {private string[] P Rops = null; Public Limitpropscontractresolver (string[] props) {this.props = props; } protected override ilist<jsonproperty> Createproperties (type type, memberserialization memberserialization) {ilist<jsonproperty> list = base. Createproperties (type, memberserialization); Isodatetimeconverter ISO = new Isodatetimeconverter () {DateTimeFormat = "yyyy-mm-dd HH:mm:ss"}; ilist<jsonproperty> listwithconver = new list<jsonproperty> (); foreach (var item in list) {if (props. Contains (item. PropertyName)) {if (item). Propertytype.tostring(). Contains ("System.DateTime")) {item. Converter = ISO; } listwithconver.add (item); }} return listwithconver; } }}
Some handling of json.net
In a manner that is helpful to all
About the http://www.codeplex.com/of josn.net
Reprinted from: http://www.cnblogs.com/yesehuaqingqian/p/3934532.html
Dynamic serialization of reprinted self---json.net and processing of time format