JSON Class
JSON Class
C#代码
public class JSON { public static string DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss"; public static string Encode(object o) { if(o == null || o.ToString() == "null") return null; if (o != null && (o.GetType() == typeof(String) || o.GetType() == typeof(string))) { return o.ToString(); } IsoDateTimeConverter dt = new IsoDateTimeConverter(); dt.DateTimeFormat = DateTimeFormat; return JsonConvert.SerializeObject(o, dt); } public static object Decode(string json) { if (String.IsNullOrEmpty(json)) return ""; object o = JsonConvert.DeserializeObject(json); if (o.GetType() == typeof(String) || o.GetType() == typeof(string)) { o = JsonConvert.DeserializeObject(o.ToString()); } object v = toObject(o); return v; } public static object Decode(string json, Type type) { return JsonConvert.DeserializeObject(json, type); } private static object toObject(object o) { if (o == null) return null; if (o.GetType() == typeof(string)) { //判断是否符合2010-09-02T10:00:00的格式 string s = o.ToString(); if (s.Length == 19 && s[10] == 'T' && s[4] == '-' && s[13] == ':') { o = System.Convert.ToDateTime(o); } } else if (o is JObject) { JObject jo = o as JObject; Hashtable h = new Hashtable(); foreach (KeyValuePair<string, JToken> entry in jo) { h[entry.Key] = toObject(entry.Value); } o = h; } else if (o is IList) { ArrayList list = new ArrayList(); list.AddRange((o as IList)); int i = 0, l = list.Count; for (; i < l; i++) { list[i] = toObject(list[i]); } o = list; } else if (typeof(JValue) == o.GetType()) { JValue v = (JValue)o; o = toObject(v.Value); } else { } return o; } public static T Deserialize<T>(string json) { T obj = Activator.CreateInstance<T>(); //using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json))) //{ // System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType()); // return (T)serializer.ReadObject(ms); //} obj= (T)Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(T)); return obj; } }
描述:负责对Object To Json 或 Json to Object的转换, 需要引入Newtonsoft.Json.Net20.dll
浙公网安备 33010602011771号