You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
239 lines
8.2 KiB
C#
239 lines
8.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Runtime.Serialization.Json;
|
|
using System.IO;
|
|
using System.Xml;
|
|
using System.Data;
|
|
using System.Collections;
|
|
using System.Web.Script.Serialization;
|
|
|
|
namespace ThriftCommon
|
|
{
|
|
/// <summary>
|
|
/// JSON序列化和反序列化辅助类
|
|
/// 李万锋
|
|
/// Create by Jacky on 2015-05-28
|
|
/// </summary>
|
|
public class JsonHelper
|
|
{
|
|
/// <summary>
|
|
/// JSON序列化
|
|
/// </summary>
|
|
public static string JsonSerializer<T>(T t)
|
|
{
|
|
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
|
|
MemoryStream ms = new MemoryStream();
|
|
ser.WriteObject(ms, t);
|
|
string jsonString = Encoding.UTF8.GetString(ms.ToArray());
|
|
ms.Close();
|
|
//替换Json的Date字符串
|
|
string p = @"\\/Date\((\d+)\+\d+\)\\/";
|
|
MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString);
|
|
Regex reg = new Regex(p);
|
|
jsonString = reg.Replace(jsonString, matchEvaluator);
|
|
//if (jsonString.StartsWith("["))
|
|
//{
|
|
// jsonString = jsonString.Remove(0,1);
|
|
//}
|
|
//if (jsonString.EndsWith("]"))
|
|
//{
|
|
// jsonString = jsonString.Remove(jsonString.Length-1);
|
|
//}
|
|
return jsonString;
|
|
}
|
|
|
|
/// <summary>
|
|
/// JSON序列化
|
|
/// </summary>
|
|
/// <param name="RootName">根名称</param>
|
|
public static string JsonSerializer<T>(T t, string RootName)
|
|
{
|
|
DataContractJsonSerializer serializer =
|
|
new DataContractJsonSerializer(typeof(T), RootName);
|
|
MemoryStream ms = new MemoryStream();
|
|
XmlDictionaryWriter w = JsonReaderWriterFactory.CreateJsonWriter(ms);
|
|
w.WriteStartElement("root");
|
|
w.WriteAttributeString("type", "object");
|
|
serializer.WriteObject(w, t);
|
|
w.WriteEndElement();
|
|
w.Flush();
|
|
string retVal = Encoding.UTF8.GetString(ms.ToArray());
|
|
ms.Dispose();
|
|
|
|
//替换Json的Date字符串
|
|
string p = @"\\/Date\((\d+)\+\d+\)\\/";
|
|
MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString);
|
|
Regex reg = new Regex(p);
|
|
retVal = reg.Replace(retVal, matchEvaluator);
|
|
return retVal;
|
|
}
|
|
|
|
/// <summary>
|
|
/// JSON反序列化
|
|
/// </summary>
|
|
public static T JsonDeserialize<T>(string jsonString)
|
|
{
|
|
//将"yyyy-MM-dd HH:mm:ss"格式的字符串转为"\/Date(1294499956278+0800)\/"格式
|
|
string p = @"\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}";
|
|
MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertDateStringToJsonDate);
|
|
Regex reg = new Regex(p);
|
|
jsonString = reg.Replace(jsonString, matchEvaluator);
|
|
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
|
|
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
|
|
T obj = (T)ser.ReadObject(ms);
|
|
return obj;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将Json序列化的时间由/Date(1294499956278+0800)转为字符串
|
|
/// </summary>
|
|
private static string ConvertJsonDateToDateString(Match m)
|
|
{
|
|
string result = string.Empty;
|
|
DateTime dt = new DateTime(1970, 1, 1);
|
|
dt = dt.AddMilliseconds(long.Parse(m.Groups[1].Value));
|
|
dt = dt.ToLocalTime();
|
|
result = dt.ToString("yyyy-MM-dd HH:mm:ss");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将时间字符串转为Json时间
|
|
/// </summary>
|
|
private static string ConvertDateStringToJsonDate(Match m)
|
|
{
|
|
string result = string.Empty;
|
|
DateTime dt = DateTime.Parse(m.Groups[0].Value);
|
|
dt = dt.ToUniversalTime();
|
|
TimeSpan ts = dt - DateTime.Parse("1970-01-01");
|
|
result = string.Format("\\/Date({0}+0800)\\/", ts.TotalMilliseconds);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Json 字符串 转换为 DataTable数据集合
|
|
/// </summary>
|
|
/// <param name="json"></param>
|
|
/// <returns></returns>
|
|
public static DataTable ToDataTable(string json)
|
|
{
|
|
DataTable dataTable = new DataTable(); //实例化
|
|
DataTable result;
|
|
try
|
|
{
|
|
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
|
|
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
|
|
ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
|
|
if (arrayList.Count > 0)
|
|
{
|
|
foreach (Dictionary<string, object> dictionary in arrayList)
|
|
{
|
|
if (dictionary.Keys.Count<string>() == 0)
|
|
{
|
|
result = dataTable;
|
|
return result;
|
|
}
|
|
|
|
foreach (string current in dictionary.Keys)
|
|
{
|
|
if (!dataTable.Columns.Contains(current))
|
|
{
|
|
dataTable.Columns.Add(current, dictionary[current].GetType());
|
|
}
|
|
}
|
|
|
|
DataRow dataRow = dataTable.NewRow();
|
|
foreach (string current in dictionary.Keys)
|
|
{
|
|
dataRow[current] = dictionary[current];
|
|
}
|
|
|
|
dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
result = dataTable;
|
|
return result;
|
|
}
|
|
|
|
|
|
// 将 DataTable 序列化成 json 字符串
|
|
public static string DataTableToJson(DataTable dt)
|
|
{
|
|
if (dt == null || dt.Rows.Count == 0)
|
|
{
|
|
return "\"\"";
|
|
}
|
|
JavaScriptSerializer myJson = new JavaScriptSerializer();
|
|
|
|
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].ToString());
|
|
}
|
|
list.Add(result);
|
|
}
|
|
return myJson.Serialize(list);
|
|
}
|
|
|
|
// 将对象序列化成 json 字符串
|
|
public static string ObjectToJson(object obj)
|
|
{
|
|
if (obj == null)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
JavaScriptSerializer myJson = new JavaScriptSerializer();
|
|
|
|
return myJson.Serialize(obj);
|
|
}
|
|
|
|
// 将 json 字符串反序列化成对象
|
|
public static object JsonToObject(string json)
|
|
{
|
|
if (string.IsNullOrEmpty(json))
|
|
{
|
|
return null;
|
|
}
|
|
JavaScriptSerializer myJson = new JavaScriptSerializer();
|
|
|
|
return myJson.DeserializeObject(json);
|
|
}
|
|
|
|
// 将 json 字符串反序列化成对象
|
|
public static T JsonToObject<T>(string json)
|
|
{
|
|
if (string.IsNullOrEmpty(json))
|
|
{
|
|
return default(T);
|
|
}
|
|
JavaScriptSerializer myJson = new JavaScriptSerializer();
|
|
myJson.MaxJsonLength = Int32.MaxValue;
|
|
return myJson.Deserialize<T>(json);
|
|
}
|
|
}
|
|
|
|
|
|
public static class JSON
|
|
{
|
|
public static T parse<T>(string jsonString)
|
|
{
|
|
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
|
|
{
|
|
return (T)new DataContractJsonSerializer(typeof(T)).ReadObject(ms);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|