Json基础转换
1 /// <summary> 2 /// json操作类 3 /// </summary> 4 public static class JsonExtend 5 { 6 /// <summary> 7 /// 对象转换为Json 8 /// </summary> 9 /// <param name="sender"></param> 10 /// <returns></returns> 11 public static string ObjToJson(this object sender) 12 { 13 IsoDateTimeConverter timeFormat = new IsoDateTimeConverter {DateTimeFormat = "yyyy-MM-dd HH:mm:ss"}; 14 return JsonConvert.SerializeObject(sender, timeFormat); 15 } 16 17 public static string ObjToJsonByScript(this object sender) 18 { 19 JavaScriptSerializer serialize = new JavaScriptSerializer(); 20 return serialize.Serialize(sender); 21 } 22 23 24 /// <summary> 25 /// Json转换为对象 26 /// </summary> 27 /// <typeparam name="T"></typeparam> 28 /// <param name="json"></param> 29 /// <returns></returns> 30 public static T JsonToObj<T>(this string json) 31 { 32 return JsonConvert.DeserializeObject<T>(json); 33 } 34 }