Json對於一個開發者來講已經不陌生了,首先確定要引用Newtonsoft.Json,對於開發者能夠在NuGet包裏面下載json
也能夠在下面連接下載ui
連接:https://pan.baidu.com/s/1yH5Yp2c9f73Bkx5bLiiiNQ
提取碼:xi64
spa
JsonHelper
using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Web.Script.Serialization; namespace Common { public class JsonHelper { /// <summary> /// 實體轉json /// </summary> /// <typeparam name="T">實體</typeparam> /// <param name="x">實體</param> /// <returns></returns> public static string EntityToJson<T>(T x) { string result = ""; try { result = JsonConvert.SerializeObject(x); } catch { result = ""; } return result; } /// <summary> /// 實體集合List轉json /// </summary> /// <typeparam name="T">實體</typeparam> /// <param name="x">實體</param> /// <returns></returns> public static string EntityListToJson<T>(List<T> x) { string result = ""; try { result = JsonConvert.SerializeObject(x); } catch { result = ""; } return result; } /// <summary> /// 實體集合list轉json_null /// </summary> /// <typeparam name="T">實體</typeparam> /// <param name="x">實體</param> /// <returns></returns> public static string EntityListToJson_null<T>(List<T> x) { string result = ""; try { var jSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; result = JsonConvert.SerializeObject(x, Formatting.Indented, jSetting); } catch { result = ""; } return result; } /// <summary> /// json轉實體 /// </summary> /// <typeparam name="T">實體</typeparam> /// <param name="json">json</param> /// <returns></returns> public static T JsonToEntity<T>(string json) { T t = default(T); try { t = (T)JsonConvert.DeserializeObject(json, typeof(T)); } catch { t = default(T); } return t; } /// <summary> /// json list轉集合List實體 /// </summary> /// <typeparam name="T">實體</typeparam> /// <param name="json">json</param> /// <returns></returns> public static List<T> JsonToListEntity<T>(string json) { List<T> list = null; try { list = (List<T>)JsonConvert.DeserializeObject(json, typeof(List<T>)); } catch { list = null; } return list; } #region 將json轉換爲DataTable /// <summary> /// 將json轉換爲DataTable /// </summary> /// <param name="strJson">獲得的json</param> /// <returns></returns> public static DataTable JsonToDataTable(string json) { JavaScriptSerializer jss = new JavaScriptSerializer(); ArrayList dic = jss.Deserialize<ArrayList>(json); DataTable dtb = new DataTable(); if (dic.Count > 0) { foreach (Dictionary<string, object> drow in dic) { if (dtb.Columns.Count == 0) { foreach (string key in drow.Keys) { try { dtb.Columns.Add(key, drow[key].GetType()); } catch (Exception) { string a=""; dtb.Columns.Add(key, a.GetType()); } } } DataRow row = dtb.NewRow(); foreach (string key in drow.Keys) { row[key] = drow[key]; } dtb.Rows.Add(row); } } return dtb; } #endregion #region 將DataTable轉換爲json public static string DataTableToJsonWithJsonNet(DataTable table) { string JsonString = string.Empty; JsonString = JsonConvert.SerializeObject(table); return JsonString; } public static T JsonToEntity<T>(object reqMessageBody) { throw new NotImplementedException(); } #endregion } }
有疑問請諮詢https://wpa.qq.com/msgrd?v=3&uin=2861510776&site=qq&menu=yescode