174 lines
6.1 KiB
C#
174 lines
6.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Reflection;
|
|
using System.Runtime.Serialization.Json;
|
|
using System.Runtime.Serialization;
|
|
using System.Web.Script.Serialization;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using System.IO;
|
|
using System.Data;
|
|
|
|
|
|
namespace BLL
|
|
{
|
|
public static class JsonHelper
|
|
{
|
|
/// <summary>
|
|
/// 将对象序列化为JSON格式
|
|
/// </summary>
|
|
/// <param name="o">对象</param>
|
|
/// <returns>json字符串</returns>
|
|
public static string SerializeObject(object o)
|
|
{
|
|
string json = JsonConvert.SerializeObject(o);
|
|
return json;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 解析JSON字符串生成对象实体
|
|
/// </summary>
|
|
/// <typeparam name="T">对象类型</typeparam>
|
|
/// <param name="json">json字符串(eg.{"ID":"112","Name":"石子儿"})</param>
|
|
/// <returns>对象实体</returns>
|
|
public static T DeserializeJsonToObject<T>(string json) where T : class
|
|
{
|
|
JsonSerializer serializer = new JsonSerializer();
|
|
StringReader sr = new StringReader(json);
|
|
object o = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
|
|
T t = o as T;
|
|
return t;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 解析JSON数组生成对象实体集合
|
|
/// </summary>
|
|
/// <typeparam name="T">对象类型</typeparam>
|
|
/// <param name="json">json数组字符串(eg.[{"ID":"112","Name":"石子儿"}])</param>
|
|
/// <returns>对象实体集合</returns>
|
|
public static List<T> DeserializeJsonToList<T>(string json) where T : class
|
|
{
|
|
JsonSerializer serializer = new JsonSerializer();
|
|
StringReader sr = new StringReader(json);
|
|
object o = serializer.Deserialize(new JsonTextReader(sr), typeof(List<T>));
|
|
List<T> list = o as List<T>;
|
|
return list;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 反序列化JSON到给定的匿名对象.
|
|
/// </summary>
|
|
/// <typeparam name="T">匿名对象类型</typeparam>
|
|
/// <param name="json">json字符串</param>
|
|
/// <param name="anonymousTypeObject">匿名对象</param>
|
|
/// <returns>匿名对象</returns>
|
|
public static T DeserializeAnonymousType<T>(string json, T anonymousTypeObject)
|
|
{
|
|
T t = JsonConvert.DeserializeAnonymousType(json, anonymousTypeObject);
|
|
return t;
|
|
}
|
|
|
|
public static string ListToJson<T>(IList<T> list, string jsonName)
|
|
{
|
|
StringBuilder Json = new StringBuilder();
|
|
if (string.IsNullOrEmpty(jsonName))
|
|
jsonName = list[0].GetType().Name;
|
|
Json.Append("{\"" + jsonName + "\":[");
|
|
if (list.Count > 0)
|
|
{
|
|
for (int i = 0; i < list.Count; i++)
|
|
{
|
|
T obj = Activator.CreateInstance<T>();
|
|
PropertyInfo[] pi = obj.GetType().GetProperties();
|
|
Json.Append("{");
|
|
for (int j = 0; j < pi.Length; j++)
|
|
{
|
|
Type type = pi[j].GetType();//GetValue(list[i], null).GetType();
|
|
Json.Append("\"" + pi[j].Name.ToString() + "\":" + String.Format(pi[j].GetType().ToString(), type));
|
|
if (j < pi.Length - 1)
|
|
{
|
|
Json.Append(",");
|
|
}
|
|
}
|
|
Json.Append("}");
|
|
if (i < list.Count - 1)
|
|
{
|
|
Json.Append(",");
|
|
}
|
|
}
|
|
}
|
|
Json.Append("]}");
|
|
return Json.ToString();
|
|
}
|
|
|
|
public static string ListToJson<T>(IList<T> list)
|
|
{
|
|
object obj = list[0];
|
|
return ListToJson<T>(list, obj.GetType().Name);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// DataTable转成Json
|
|
/// </summary>
|
|
/// <param name="jsonName"></param>
|
|
/// <param name="dt"></param>
|
|
/// <returns></returns>
|
|
public static string DataTableToJson(DataTable dt, string jsonName)
|
|
{
|
|
StringBuilder Json = new StringBuilder();
|
|
if (string.IsNullOrEmpty(jsonName))
|
|
jsonName = dt.TableName;
|
|
Json.Append("{\"" + jsonName + "\":[");
|
|
if (dt.Rows.Count > 0)
|
|
{
|
|
for (int i = 0; i < dt.Rows.Count; i++)
|
|
{
|
|
Json.Append("{");
|
|
for (int j = 0; j < dt.Columns.Count; j++)
|
|
{
|
|
Type type = dt.Rows[i][j].GetType();
|
|
Json.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + String.Format(dt.Rows[i][j].ToString(), type));
|
|
if (j < dt.Columns.Count - 1)
|
|
{
|
|
Json.Append(",");
|
|
}
|
|
}
|
|
Json.Append("}");
|
|
if (i < dt.Rows.Count - 1)
|
|
{
|
|
Json.Append(",");
|
|
}
|
|
}
|
|
}
|
|
Json.Append("]}");
|
|
return Json.ToString();
|
|
}
|
|
|
|
|
|
public static string DataTableToJson(DataTable dt)
|
|
{
|
|
string JsonString = string.Empty;
|
|
if (dt.Rows.Count > 0)
|
|
{
|
|
//var serializer = new JavaScriptSerializer();
|
|
//serializer.MaxJsonLength = Int32.MaxValue; //设置为int的最大值
|
|
// JsonString = serializer.Serialize(JsonConvert.SerializeObject(dt));
|
|
JsonString = JsonConvert.SerializeObject(dt);
|
|
}
|
|
return JsonString;
|
|
}
|
|
|
|
//public static void Main()
|
|
//{
|
|
// var str = "{name : \"json\", age : 15}";
|
|
// JObject obj = JObject.Parse(str);
|
|
// string s1 = obj["name"].ToString();
|
|
// string s2 = obj["age"].ToString();
|
|
//}
|
|
|
|
}
|
|
}
|