123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
-
- 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
- {
-
-
-
- public class JsonHelper
- {
- #region DataTable<-->JSON
-
-
-
-
-
- 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);
- }
-
-
-
-
-
-
- public static string SerializeToJson(object obj, int recursionLimit = 100)
- {
- try
- {
- JavaScriptSerializer serialize = new JavaScriptSerializer();
- serialize.RecursionLimit = recursionLimit;
- return serialize.Serialize(obj);
- }
- catch { return ""; }
- }
-
-
-
-
-
- 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)
- {
- 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
-
-
-
-
- public static string ToJson(object t)
- {
- return JsonConvert.SerializeObject(t, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include });
- }
-
-
-
-
-
-
- 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);
- }
-
-
-
-
-
-
- public static T FromJson<T>(string strJson) where T : class
- {
- if (!strJson.IsNullOrEmpty())
- return JsonConvert.DeserializeObject<T>(strJson);
- return null;
- }
-
-
-
-
-
-
- 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
-
-
-
- public string Status { get; set; }
-
-
-
- public string Msg { get; set; }
-
-
-
- public string ReUrl { get; set; }
-
-
-
- public object Data { get; set; }
- #endregion
- }
- }
|