c# Json操作
Posted yuanzijian-ruiec
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c# Json操作相关的知识,希望对你有一定的参考价值。
using System; using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using System.Reflection; using System.Data; using Newtonsoft.Json.Linq; namespace EasyFrame.Common { /// <summary> /// Json操作 /// </summary> public static class Json { public static string ToJson(this object obj, string datetimeformats) { var timeConverter = new IsoDateTimeConverter { DateTimeFormat = datetimeformats }; return JsonConvert.SerializeObject(obj, timeConverter); } public static List<T> ToList<T>(this string Json) { return Json == null ? null : JsonConvert.DeserializeObject<List<T>>(Json); } public static DataTable ToTable(this string Json) { return Json == null ? null : JsonConvert.DeserializeObject<DataTable>(Json); } public static JObject ToJObject(this string Json) { return Json == null ? JObject.Parse("{}") : JObject.Parse(Json.Replace(" ", "")); } #region 值转换 private static object convertVal(Type t, object data) { object val = null; if (t == typeof(Int32)) val = Convert.ToInt32(data); else if (t == typeof(DateTime)) val = Convert.ToDateTime(data); else if (t == typeof(Decimal)) val = Convert.ToDecimal(data); else if (t == typeof(Boolean)) val = Convert.ToBoolean(data); else if (t == typeof(String)) val = Convert.ToString(data).Trim(); else if (t == typeof(Guid)) val = new Guid(data.ToString()); else val = data; return val; } #endregion #region IToJson 成员 public static string ToJson(object model) { if (model == null) return null; string value = JsonConvert.SerializeObject(model, new AspNetDateTimeConverter()); return value; } public static string ToJson(System.Data.DataTable dt) { string value = JsonConvert.SerializeObject(dt); return value; } public static string ToJson(string data) { object[] objs = new object[] { data }; return ToJson(objs); } public static string ToJson(int data) { object[] objs = new object[] { data }; return ToJson(objs); } public static string ToJson(decimal data) { object[] objs = new object[] { data }; return ToJson(objs); } public static string ToJson(bool data) { object[] objs = new object[] { data }; return ToJson(objs); } public static string ToJson(DateTime data) { object[] objs = new object[] { data }; return ToJson(objs); } public static string ToJson(params object[] data) { string value = JsonConvert.SerializeObject(data, new AspNetDateTimeConverter()); return value; } #endregion #region IJsonToObject成员 public static T ToObject<T>(string json) { return JsonConvert.DeserializeObject<T>(json); } public static object ToObject(string json) { return JsonConvert.DeserializeObject(json); } public static object[] ToArray(string json) { return (ToObject(json) as JArray).ToArray(); } public static string ToString(string json) { return Convert.ToString(ToArray(json)[0]); } public static bool ToBoolean(string json) { return Convert.ToBoolean(ToArray(json)[0]); } public static int ToInt32(string json) { return Convert.ToInt32(ToArray(json)[0]); } public static decimal ToDecimal(string json) { return Convert.ToDecimal(ToArray(json)[0]); } public static DateTime ToDateTime(string json) { return Convert.ToDateTime(ToArray(json)[0]); } public static T ToObject<T>(object data) { if (typeof(T).Equals(typeof(int[]))) { List<int> intvals = new List<int>(); for (int i = 0; i < (data as JArray).Count; i++) { intvals.Add(Convert.ToInt32((data as JArray)[i])); } return (T)(intvals.ToArray() as object); } else if (typeof(T).Equals(typeof(string[]))) { List<string> intvals = new List<string>(); for (int i = 0; i < (data as JArray).Count; i++) { intvals.Add(Convert.ToString((data as JArray)[i])); } return (T)(intvals.ToArray() as object); } else if (typeof(T).Equals(typeof(decimal[]))) { List<decimal> intvals = new List<decimal>(); for (int i = 0; i < (data as JArray).Count; i++) { intvals.Add(Convert.ToDecimal((data as JArray)[i])); } return (T)(intvals.ToArray() as object); } else if (typeof(T).Equals(typeof(Boolean[]))) { List<Boolean> intvals = new List<Boolean>(); for (int i = 0; i < (data as JArray).Count; i++) { intvals.Add(Convert.ToBoolean((data as JArray)[i])); } return (T)(intvals.ToArray() as object); } else if (typeof(T).Equals(typeof(DateTime[]))) { List<DateTime> intvals = new List<DateTime>(); for (int i = 0; i < (data as JArray).Count; i++) { intvals.Add(Convert.ToDateTime((data as JArray)[i])); } return (T)(intvals.ToArray() as object); } else if (typeof(T).Equals(typeof(object[]))) { List<object> intvals = new List<object>(); for (int i = 0; i < (data as JArray).Count; i++) { intvals.Add((data as JArray)[i]); } return (T)(intvals.ToArray() as object); } else { T obj = (T)Activator.CreateInstance(typeof(T)); PropertyInfo[] pros = typeof(T).GetProperties(); var oldinttype = typeof(Nullable<int>); var olddectype = typeof(Nullable<decimal>); var olddoutype = typeof(Nullable<double>); var oldbooltype = typeof(Nullable<Boolean>); var newinttype = typeof(int); var newdectype = typeof(decimal); var newdoutype = typeof(double); var newbooltype = typeof(Boolean); for (int k = 0; k < pros.Length; k++) { object val; if (pros[k].PropertyType.Equals(oldinttype)) { if (pros[k].Name == null) val = default(int); else val = convertVal(newinttype, (data as JObject)[pros[k].Name]); } else if (pros[k].PropertyType.Equals(olddectype)) { if (pros[k].Name == null) val = default(decimal); else val = convertVal(newdectype, (data as JObject)[pros[k].Name]); } else if (pros[k].PropertyType.Equals(olddoutype)) { if (pros[k].Name == null) val = default(double); else val = convertVal(newdoutype, (data as JObject)[pros[k].Name]); } else if (pros[k].PropertyType.Equals(oldbooltype)) { if (pros[k].Name == null) val = default(bool); else val = convertVal(newbooltype, (data as JObject)[pros[k].Name]); } else { val = convertVal(pros[k].PropertyType, (data as JObject)[pros[k].Name]); } pros[k].SetValue(obj, val, null); } return obj; } } public static object[] ToArray(object data) { return (data as JArray).ToArray(); } public static List<T> ToListObj<T>(object data) { if (data is JArray) { PropertyInfo[] pros = typeof(T).GetProperties(); List<T> list = new List<T>(); for (int i = 0; i < (data as JArray).Count; i++) { T obj = (T)Activator.CreateInstance(typeof(T)); object _data = (data as JArray)[i]; for (int k = 0; k < pros.Length; k++) { object val = convertVal(pros[k].PropertyType, (_data as JObject)[pros[k].Name]); pros[k].SetValue(obj, val, null); } list.Add(obj); } return list; } return null; } public static DataTable ToDataTable(object data) { if (data is JArray && (data as JArray).Count > 0) { JObject _data = (data as JArray)[0] as JObject; DataTable dt = new DataTable(); foreach (var name in _data) { dt.Columns.Add(name.Key, _data[name.Key].GetType()); } for (int i = 0; i < (data as JArray).Count; i++) { object _jsarray = (data as JArray)[i]; DataRow dr = dt.NewRow(); for (int k = 0; k < dt.Columns.Count; k++) { dr[k] = convertVal(dt.Columns[k].DataType, (_jsarray as JObject)[dt.Columns[k].ColumnName]); } dt.Rows.Add(dr); } return dt; } return null; } public static DataTable ToDataTable(string data) { return ToDataTable(JsonConvert.DeserializeObject(data)); } #endregion } public class AspNetDateTimeConverter : javascriptDateTimeConverter { public override bool CanConvert(Type objectType) { return typeof(DateTime).IsAssignableFrom(objectType); } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { DateTime datetime = Convert.ToDateTime(value); writer.WriteValue(datetime.ToString("yyyy-MM-dd HH:mm:ss")); } } }
以上是关于c# Json操作的主要内容,如果未能解决你的问题,请参考以下文章