JsonHelper
Posted loushengjie
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JsonHelper相关的知识,希望对你有一定的参考价值。
Json对于一个开发者来说已经不陌生了,首先肯定要引用Newtonsoft.Json,对于开发者可以在NuGet包里面下载
也可以在下面链接下载
链接:https://pan.baidu.com/s/1yH5Yp2c9f73Bkx5bLiiiNQ
提取码:xi64
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=yes
以上是关于JsonHelper的主要内容,如果未能解决你的问题,请参考以下文章