123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- /* 作者: 季健国
- * 创建时间: 2012/6/9 23:10:11
- *
- */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Web.Script.Serialization;
- using Newtonsoft.Json;
- using System.Xml;
- using System.Runtime.Serialization.Json;
- using System.IO;
- using System.Data;
- using System.Collections;
- namespace Ant.Service.Common
- {
- /// <summary>
- /// 提供了一个关于json的辅助类
- /// </summary>
- public class JsonHelper
- {
- #region DataTable<-->JSON
- /// <summary>
- /// DataTable转为json
- /// </summary>
- /// <param name="dt">DataTable</param>
- /// <returns>json数据</returns>
- public static string DataTableToJson(DataTable dt)
- {
- List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
- foreach (DataRow dr in dt.Rows)
- {
- Dictionary<string, object> result = new Dictionary<string, object>();
- foreach (DataColumn dc in dt.Columns)
- {
- result.Add(dc.ColumnName, dr[dc]);
- }
- list.Add(result);
- }
- return SerializeToJson(list);
- }
- /// <summary>
- /// 序列化对象为Json字符串
- /// </summary>
- /// <param name="obj">要序列化的对象</param>
- /// <param name="recursionLimit">序列化对象的深度,默认为100</param>
- /// <returns>Json字符串</returns>
- public static string SerializeToJson(object obj, int recursionLimit = 100)
- {
- try
- {
- JavaScriptSerializer serialize = new JavaScriptSerializer();
- serialize.RecursionLimit = recursionLimit;
- return serialize.Serialize(obj);
- }
- catch { return ""; }
- }
- /// <summary>
- /// json包转DataTable
- /// </summary>
- /// <param name="jsons"></param>
- /// <returns></returns>
- public static DataTable JsonToDataTable(string jsons)
- {
- DataTable dt = new DataTable();
- try
- {
- JavaScriptSerializer serialize = new JavaScriptSerializer();
- serialize.MaxJsonLength = Int32.MaxValue;
- ArrayList list = serialize.Deserialize<ArrayList>(jsons);
- if (list.Count > 0)
- {
- foreach (Dictionary<string, object> item in list)
- {
- if (item.Keys.Count == 0)//无值返回空
- {
- return dt;
- }
- if (dt.Columns.Count == 0)//初始Columns
- {
- foreach (string current in item.Keys)
- {
- dt.Columns.Add(current, item[current].GetType());
- }
- }
- DataRow dr = dt.NewRow();
- foreach (string current in item.Keys)
- {
- dr[current] = item[current];
- }
- dt.Rows.Add(dr);
- }
- }
- }
- catch
- {
- return dt;
- }
- return dt;
- }
- #endregion
- #region Method
- /// <summary>
- /// 类对像转换成json格式
- /// </summary>
- /// <returns></returns>
- public static string ToJson(object t)
- {
- return JsonConvert.SerializeObject(t, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include });
- }
- /// <summary>
- /// 类对像转换成json格式
- /// </summary>
- /// <param name="t"></param>
- /// <param name="HasNullIgnore">是否忽略NULL值</param>
- /// <returns></returns>
- public static string ToJson(object t, bool HasNullIgnore)
- {
- if (HasNullIgnore)
- return JsonConvert.SerializeObject(t, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
- else
- return ToJson(t);
- }
- /// <summary>
- /// json格式转换
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="strJson"></param>
- /// <returns></returns>
- public static T FromJson<T>(string strJson) where T : class
- {
- if (!strJson.IsNullOrEmpty())
- return JsonConvert.DeserializeObject<T>(strJson);
- return null;
- }
- /// <summary>
- /// 功能描述:将List转换为Json
- /// 创建标识:szj
- /// </summary>
- /// <param name="a"></param>
- /// <returns></returns>
- public static string ListToJson(IList<object> a)
- {
- DataContractJsonSerializer json = new DataContractJsonSerializer(a.GetType());
- string szJson = "";
- //序列化
- using (MemoryStream stream = new MemoryStream())
- {
- json.WriteObject(stream, a);
- szJson = Encoding.UTF8.GetString(stream.ToArray());
- }
- return szJson;
- }
- #endregion
- #region Property
- /// <summary>
- /// 数据状态
- /// </summary>
- public string Status { get; set; }
- /// <summary>
- /// 提示信息
- /// </summary>
- public string Msg { get; set; }
- /// <summary>
- /// 回传URL
- /// </summary>
- public string ReUrl { get; set; }
- /// <summary>
- /// 数据包
- /// </summary>
- public object Data { get; set; }
- #endregion
- }
- }
|