766 lines
27 KiB
C#
766 lines
27 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Text;
|
||
using System.Data;
|
||
using System.Collections;
|
||
using System.Reflection;
|
||
using System.IO;
|
||
using System.Web.Script.Serialization;
|
||
using QRWL.Web.Common;
|
||
using Newtonsoft.Json.Linq;
|
||
using Newtonsoft.Json;
|
||
using Newtonsoft.Json.Converters;
|
||
|
||
namespace QRWL.Common
|
||
{
|
||
public class JsonHelper
|
||
{
|
||
|
||
public static string ArrayToJson(string[] strs)
|
||
{
|
||
StringBuilder sb = new StringBuilder();
|
||
for (int i = 0; i < strs.Length; i++)
|
||
{
|
||
sb.AppendFormat("'{0}':'{1}',", i + 1, strs[i]);
|
||
}
|
||
if (sb.Length > 0)
|
||
{
|
||
return ("{" + sb.ToString().TrimEnd(new char[] { ',' }) + "}");
|
||
}
|
||
return "";
|
||
}
|
||
|
||
public static string ArrayToJson<T>(List<T> list, string propertyname)
|
||
{
|
||
StringBuilder sb = new StringBuilder();
|
||
if (list.Count > 0)
|
||
{
|
||
sb.Append("[{\"");
|
||
sb.Append(propertyname);
|
||
sb.Append("\":[");
|
||
foreach (T t in list)
|
||
{
|
||
sb.Append("\"");
|
||
sb.Append(t.ToString());
|
||
sb.Append("\",");
|
||
}
|
||
return (sb.ToString().TrimEnd(new char[] { ',' }) + "]}]");
|
||
}
|
||
return "";
|
||
}
|
||
|
||
public static string DataRowToJson(DataRow dr)
|
||
{
|
||
StringBuilder sb = new StringBuilder();
|
||
sb.Append("{");
|
||
foreach (DataColumn dc in dr.Table.Columns)
|
||
{
|
||
sb.Append("\"");
|
||
sb.Append(dc.ColumnName);
|
||
sb.Append("\":\"");
|
||
if (((dr[dc] != null) && (dr[dc] != DBNull.Value)) && (dr[dc].ToString() != ""))
|
||
{
|
||
sb.Append(dr[dc]);
|
||
}
|
||
else
|
||
{
|
||
sb.Append(" ");
|
||
}
|
||
sb.Append("\",");
|
||
}
|
||
sb = sb.Remove(0, sb.Length - 1);
|
||
sb.Append("},");
|
||
return sb.ToString();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Datatable转换为Json
|
||
/// </summary>
|
||
/// <param name="table">Datatable对象</param>
|
||
/// <returns>Json字符串</returns>
|
||
public static string DataTableToJson(DataTable dt)
|
||
{
|
||
StringBuilder jsonString = new StringBuilder();
|
||
jsonString.Append("[");
|
||
DataRowCollection drc = dt.Rows;
|
||
for (int i = 0; i < drc.Count; i++)
|
||
{
|
||
jsonString.Append("{");
|
||
for (int j = 0; j < dt.Columns.Count; j++)
|
||
{
|
||
string strKey = dt.Columns[j].ColumnName;
|
||
string strValue = drc[i][j].ToString();
|
||
Type type = dt.Columns[j].DataType;
|
||
jsonString.Append("\"" + strKey + "\":");
|
||
strValue = StringFormat(strValue, type);
|
||
if (j < dt.Columns.Count - 1)
|
||
{
|
||
jsonString.Append(strValue + ",");
|
||
}
|
||
else
|
||
{
|
||
jsonString.Append(strValue);
|
||
}
|
||
}
|
||
jsonString.Append("},");
|
||
}
|
||
jsonString.Remove(jsonString.Length - 1, 1);
|
||
jsonString.Append("]");
|
||
if (jsonString.Length == 1)
|
||
{
|
||
return "[]";
|
||
}
|
||
return jsonString.ToString();
|
||
}
|
||
|
||
public static string DataTableToJson(DataTable dt, string dtName)
|
||
{
|
||
StringBuilder sb = new StringBuilder();
|
||
sb.Append("{\"");
|
||
sb.Append(dtName);
|
||
sb.Append("\":[");
|
||
if (DataTableHelper.IsExistRows(dt))
|
||
{
|
||
foreach (DataRow dr in dt.Rows)
|
||
{
|
||
sb.Append("{");
|
||
foreach (DataColumn dc in dr.Table.Columns)
|
||
{
|
||
sb.Append("\"");
|
||
sb.Append(dc.ColumnName);
|
||
sb.Append("\":\"");
|
||
if (((dr[dc] != null) && (dr[dc] != DBNull.Value)) && (dr[dc].ToString() != ""))
|
||
{
|
||
sb.Append(dr[dc].ToString().Replace("\t", " ").Replace("\r\n", " ").Replace(
|
||
"\"", "'").Replace("'", "“")).Replace(@"\", "/");
|
||
}
|
||
else
|
||
{
|
||
sb.Append(" ");
|
||
}
|
||
sb.Append("\",");
|
||
}
|
||
sb = sb.Remove(sb.Length - 1, 1);
|
||
sb.Append("},");
|
||
}
|
||
sb = sb.Remove(sb.Length - 1, 1);
|
||
}
|
||
sb.Append("]}");
|
||
return JsonCharFilter(sb.ToString());
|
||
}
|
||
|
||
private static List<string> GetObjectProperty(object o)
|
||
{
|
||
List<string> propertyslist = new List<string>();
|
||
PropertyInfo[] propertys = o.GetType().GetProperties();
|
||
foreach (PropertyInfo p in propertys)
|
||
{
|
||
propertyslist.Add(string.Concat(new object[] { "\"", p.Name.ToString(), "\":\"", p.GetValue(o, null), "\"" }));
|
||
}
|
||
return propertyslist;
|
||
}
|
||
|
||
public static string HashtableToJson(Hashtable data, string dtName)
|
||
{
|
||
StringBuilder sb = new StringBuilder();
|
||
sb.Append("{\"");
|
||
sb.Append(dtName);
|
||
sb.Append("\":[{");
|
||
foreach (object key in data.Keys)
|
||
{
|
||
object value = data[key];
|
||
sb.Append("\"");
|
||
sb.Append(key);
|
||
sb.Append("\":\"");
|
||
if (!(string.IsNullOrEmpty(value.ToString()) || (value == DBNull.Value)))
|
||
{
|
||
sb.Append(value).Replace(@"\", "/");
|
||
}
|
||
else
|
||
{
|
||
sb.Append(" ");
|
||
}
|
||
sb.Append("\",");
|
||
}
|
||
sb = sb.Remove(sb.Length - 1, 1);
|
||
sb.Append("}]}");
|
||
return JsonCharFilter(sb.ToString());
|
||
}
|
||
|
||
public static string IListToJson<T>(IList<T> list, bool FlagBool = false)
|
||
{
|
||
StringBuilder sb = new StringBuilder();
|
||
sb.Append("[");
|
||
foreach (T t in list)
|
||
{
|
||
sb.Append(ObjectToJson<T>(t, FlagBool) + ",");
|
||
}
|
||
return (sb.ToString().TrimEnd(new char[] { ',' }) + "]");
|
||
}
|
||
|
||
public static string IListToJson<T>(IList<T> list, string ClassName)
|
||
{
|
||
StringBuilder sb = new StringBuilder();
|
||
sb.Append("{\"" + ClassName + "\":[");
|
||
foreach (T t in list)
|
||
{
|
||
sb.Append(ObjectToJson<T>(t) + ",");
|
||
}
|
||
return (sb.ToString().TrimEnd(new char[] { ',' }) + "]}");
|
||
}
|
||
|
||
private static string JsonCharFilter(string sourceStr)
|
||
{
|
||
return sourceStr;
|
||
}
|
||
|
||
|
||
public static string ListToJson<T>(List<T> objlist, string jsonName, bool ObjectName = true)
|
||
{
|
||
string result = string.Empty;
|
||
if (ObjectName)
|
||
{
|
||
result += "{";
|
||
if (jsonName.Equals(string.Empty))
|
||
{
|
||
object o = objlist[0];
|
||
jsonName = o.GetType().ToString();
|
||
result = result + "\"" + jsonName + "\":[";
|
||
}
|
||
else
|
||
{
|
||
result = result + "\"" + jsonName + "\":[";
|
||
}
|
||
}
|
||
else
|
||
{
|
||
result = "[";
|
||
}
|
||
|
||
bool firstline = true;
|
||
foreach (object oo in objlist)
|
||
{
|
||
if (!firstline)
|
||
{
|
||
result = result + "," + ObjectToJson(oo);
|
||
}
|
||
else
|
||
{
|
||
result = result + ObjectToJson(oo);
|
||
firstline = false;
|
||
}
|
||
}
|
||
if (ObjectName)
|
||
{
|
||
return (result + "]}");
|
||
}
|
||
else
|
||
{
|
||
return (result + "]");
|
||
}
|
||
}
|
||
|
||
private static string ObjectToJson(object o)
|
||
{
|
||
string result = "{";
|
||
List<string> ls_propertys = new List<string>();
|
||
ls_propertys = GetObjectProperty(o);
|
||
foreach (string str_property in ls_propertys)
|
||
{
|
||
if (result.Equals("{"))
|
||
{
|
||
result = result + str_property;
|
||
}
|
||
else
|
||
{
|
||
result = result + "," + str_property;
|
||
}
|
||
}
|
||
return (result + "}");
|
||
}
|
||
|
||
public static string ObjectToJson<T>(T t, bool FlagBool = false)
|
||
{
|
||
FlagBool = false;
|
||
StringBuilder sb = new StringBuilder();
|
||
string json = "";
|
||
if (t == null)
|
||
{
|
||
return json;
|
||
}
|
||
sb.Append("{");
|
||
PropertyInfo[] properties = t.GetType().GetProperties();
|
||
foreach (PropertyInfo pi in properties)
|
||
{
|
||
sb.Append("\"" + pi.Name.ToString() + "\"");
|
||
sb.Append(":");
|
||
if (FlagBool == true && pi.PropertyType.ToString() == "System.Boolean")
|
||
{
|
||
sb.Append(pi.GetValue(t, null));
|
||
}
|
||
else
|
||
{
|
||
sb.Append("\"" + pi.GetValue(t, null) + "\"");
|
||
}
|
||
sb.Append(",");
|
||
}
|
||
return (sb.ToString().TrimEnd(new char[] { ',' }) + "}");
|
||
}
|
||
|
||
public static string ObjectToJson<T>(T t, string ClassName)
|
||
{
|
||
StringBuilder sb = new StringBuilder();
|
||
sb.Append("{\"" + ClassName + "\":[");
|
||
string json = "";
|
||
if (t == null)
|
||
{
|
||
return json;
|
||
}
|
||
sb.Append("{");
|
||
PropertyInfo[] properties = t.GetType().GetProperties();
|
||
foreach (PropertyInfo pi in properties)
|
||
{
|
||
sb.Append("\"" + pi.Name.ToString() + "\"");
|
||
sb.Append(":");
|
||
sb.Append("\"" + pi.GetValue(t, null) + "\"");
|
||
sb.Append(",");
|
||
}
|
||
return (sb.ToString().TrimEnd(new char[] { ',' }) + "}]}");
|
||
}
|
||
|
||
public static string ObjectToJson<T>(IList<T> IL, string jsonName)
|
||
{
|
||
StringBuilder Json = new StringBuilder();
|
||
Json.Append("{\"" + jsonName + "\":[");
|
||
if (IL.Count > 0)
|
||
{
|
||
for (int i = 0; i < IL.Count; i++)
|
||
{
|
||
PropertyInfo[] pis = Activator.CreateInstance<T>().GetType().GetProperties();
|
||
Json.Append("{");
|
||
for (int j = 0; j < pis.Length; j++)
|
||
{
|
||
Json.Append(string.Concat(new object[] { "\"", pis[j].Name.ToString(), "\":\"", pis[j].GetValue(IL[i], null), "\"" }));
|
||
if (j < (pis.Length - 1))
|
||
{
|
||
Json.Append(",");
|
||
}
|
||
}
|
||
Json.Append("}");
|
||
if (i < (IL.Count - 1))
|
||
{
|
||
Json.Append(",");
|
||
}
|
||
}
|
||
}
|
||
Json.Append("]}");
|
||
return Json.ToString();
|
||
}
|
||
|
||
/// <summary>
|
||
/// JSON格式数组转化为对应的List<T>
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="JsonStr">JSON格式数组</param>
|
||
/// <returns></returns>
|
||
public static List<T> JSONStringToList<T>(string JsonStr)
|
||
{
|
||
JavaScriptSerializer Serializer = new JavaScriptSerializer();
|
||
//设置转化JSON格式时字段长度
|
||
List<T> objs = Serializer.Deserialize<List<T>>(JsonStr);
|
||
return objs;
|
||
}
|
||
|
||
|
||
public static Object Json2Obj(String json, Type t)
|
||
{
|
||
|
||
try
|
||
{
|
||
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer =
|
||
new System.Runtime.Serialization.Json.DataContractJsonSerializer(t);
|
||
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
|
||
{
|
||
return serializer.ReadObject(ms);
|
||
}
|
||
}
|
||
catch
|
||
{
|
||
return null;
|
||
}
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// DataSet转换为Json
|
||
/// </summary>
|
||
/// <param name="dataSet">DataSet对象</param>
|
||
/// <returns>Json字符串</returns>
|
||
public static string DataSetToJson(DataSet dataSet)
|
||
{
|
||
string jsonString = "{";
|
||
foreach (DataTable table in dataSet.Tables)
|
||
{
|
||
jsonString += "\"" + table.TableName + "\":" + DataTableToJson(table) + ",";
|
||
}
|
||
jsonString = jsonString.TrimEnd(',');
|
||
return jsonString + "}";
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将JSON解析成DataSet只限标准的JSON数据
|
||
/// 例如:Json={t1:[{name:'数据name',type:'数据type'}]}
|
||
/// 或 Json={t1:[{name:'数据name',type:'数据type'}],t2:[{id:'数据id',gx:'数据gx',val:'数据val'}]}
|
||
/// </summary>
|
||
/// <param name="Json">Json字符串</param>
|
||
/// <returns>DataSet</returns>
|
||
public static DataSet JsonToDataSet(string Json)
|
||
{
|
||
try
|
||
{
|
||
DataSet ds = new DataSet();
|
||
JavaScriptSerializer JSS = new JavaScriptSerializer();
|
||
object obj = JSS.DeserializeObject(Json);
|
||
Dictionary<string, object> datajson = (Dictionary<string, object>)obj;
|
||
foreach (var item in datajson)
|
||
{
|
||
DataTable dt = new DataTable(item.Key);
|
||
object[] rows = (object[])item.Value;
|
||
foreach (var row in rows)
|
||
{
|
||
Dictionary<string, object> val = (Dictionary<string, object>)row;
|
||
DataRow dr = dt.NewRow();
|
||
foreach (KeyValuePair<string, object> sss in val)
|
||
{
|
||
if (!dt.Columns.Contains(sss.Key))
|
||
{
|
||
dt.Columns.Add(sss.Key.ToString());
|
||
dr[sss.Key] = sss.Value;
|
||
}
|
||
else
|
||
dr[sss.Key] = sss.Value;
|
||
}
|
||
dt.Rows.Add(dr);
|
||
}
|
||
ds.Tables.Add(dt);
|
||
}
|
||
return ds;
|
||
}
|
||
catch
|
||
{
|
||
return null;
|
||
}
|
||
}
|
||
|
||
|
||
#region 方法 -> Json 字符串 转换为 DataTable数据集合
|
||
/// <summary>
|
||
/// Json 字符串 转换为 DataTable数据集合
|
||
/// </summary>
|
||
/// <param name="json"></param>
|
||
/// <returns></returns>
|
||
public static DataTable JsonToDataTable(string json)
|
||
{
|
||
DataTable dataTable = new DataTable(); //实例化
|
||
DataTable result;
|
||
try
|
||
{
|
||
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
|
||
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
|
||
if (!string.IsNullOrEmpty(json))
|
||
{
|
||
ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
|
||
if (arrayList.Count > 0)
|
||
{
|
||
foreach (Dictionary<string, object> dictionary in arrayList)
|
||
{
|
||
if (dictionary.Keys.Count == 0)
|
||
{
|
||
result = dataTable;
|
||
return result;
|
||
}
|
||
//Columns
|
||
if (dataTable.Columns.Count == 0)
|
||
{
|
||
foreach (string current in dictionary.Keys)
|
||
{
|
||
if (IsType(dictionary[current].GetType(), "System.Nullable`1[System.Int16]") ||
|
||
IsType(dictionary[current].GetType(), "System.Nullable`1[System.Int32]") ||
|
||
IsType(dictionary[current].GetType(), "System.Nullable`1[System.Int64]") ||
|
||
IsType(dictionary[current].GetType(), "System.Nullable`1[System.Double]") ||
|
||
IsType(dictionary[current].GetType(), "System.Nullable`1[System.Decimal]") ||
|
||
dictionary[current].GetType() == typeof(System.Int16) ||
|
||
dictionary[current].GetType() == typeof(System.Int32) ||
|
||
dictionary[current].GetType() == typeof(System.Int64) ||
|
||
dictionary[current].GetType() == typeof(System.Double) ||
|
||
dictionary[current].GetType() == typeof(System.Decimal))
|
||
{
|
||
dataTable.Columns.Add(current, typeof(System.Decimal));
|
||
}
|
||
else
|
||
{
|
||
dataTable.Columns.Add(current, dictionary[current].GetType());
|
||
}
|
||
}
|
||
}
|
||
//Rows
|
||
DataRow dataRow = dataTable.NewRow();
|
||
foreach (string current in dictionary.Keys)
|
||
{
|
||
try
|
||
{
|
||
dataRow[current] = dictionary[current];
|
||
}
|
||
catch { }
|
||
}
|
||
dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
SuperMap.RealEstate.Utility.ErrorLogHelper.Write(ex);
|
||
}
|
||
result = dataTable;
|
||
return result;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 类型匹配
|
||
/// <summary>
|
||
/// 类型匹配
|
||
/// </summary>
|
||
/// <param name="type">类型</param>
|
||
/// <param name="typeName">类型名称</param>
|
||
/// <returns></returns>
|
||
public static bool IsType(Type type, string typeName)
|
||
{
|
||
if (type.ToString() == typeName)
|
||
return true;
|
||
if (type.ToString() == "System.Object")
|
||
return false;
|
||
return IsType(type.BaseType, typeName);
|
||
}
|
||
#endregion
|
||
|
||
/// <summary>
|
||
/// 过滤特殊字符
|
||
/// </summary>
|
||
/// <param name="s"></param>
|
||
/// <returns></returns>
|
||
public static string String2Json(String s)
|
||
{
|
||
StringBuilder sb = new StringBuilder();
|
||
for (int i = 0; i < s.Length; i++)
|
||
{
|
||
char c = s.ToCharArray()[i];
|
||
switch (c)
|
||
{
|
||
case '\"':
|
||
sb.Append("\\\""); break;
|
||
case '\\':
|
||
sb.Append("\\\\"); break;
|
||
case '/':
|
||
sb.Append("\\/"); break;
|
||
case '\b':
|
||
sb.Append("\\b"); break;
|
||
case '\f':
|
||
sb.Append("\\f"); break;
|
||
case '\n':
|
||
sb.Append("\\n"); break;
|
||
case '\r':
|
||
sb.Append("\\r"); break;
|
||
case '\t':
|
||
sb.Append("\\t"); break;
|
||
case '\v':
|
||
sb.Append("\\v"); break;
|
||
case '\0':
|
||
sb.Append("\\0"); break;
|
||
default:
|
||
sb.Append(c); break;
|
||
}
|
||
}
|
||
return sb.ToString();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 格式化字符型、日期型、布尔型
|
||
/// </summary>
|
||
/// <param name="str"></param>
|
||
/// <param name="type"></param>
|
||
/// <returns></returns>
|
||
private static string StringFormat(string str, Type type)
|
||
{
|
||
if (type != typeof(string) && string.IsNullOrEmpty(str))
|
||
{
|
||
str = "\"" + str + "\"";
|
||
}
|
||
else if (type == typeof(string))
|
||
{
|
||
str = String2Json(str);
|
||
str = "\"" + str + "\"";
|
||
}
|
||
else if (type == typeof(DateTime))
|
||
{
|
||
str = "\"" + str + "\"";
|
||
}
|
||
else if (type == typeof(bool))
|
||
{
|
||
str = str.ToLower();
|
||
}
|
||
else if (type == typeof(byte[]))
|
||
{
|
||
str = "\"" + str + "\"";
|
||
}
|
||
else if (type == typeof(Guid))
|
||
{
|
||
str = "\"" + str + "\"";
|
||
}
|
||
return str;
|
||
}
|
||
|
||
#region 方法 -> Json字符串转成list对象
|
||
public static JArray GetData2JArray(string jsonData, string key)
|
||
{
|
||
JObject obj = JObject.Parse(jsonData);
|
||
return (JArray)obj[key];
|
||
}
|
||
#endregion
|
||
|
||
public static string SerializeObject<T>(List<T> objlist, string jsonName)
|
||
{
|
||
IsoDateTimeConverter timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy'-'MM'-'dd HH':'mm':'ss" };
|
||
string reString = Newtonsoft.Json.JsonConvert.SerializeObject(objlist, Formatting.Indented, timeConverter);
|
||
if (!string.IsNullOrEmpty(jsonName))
|
||
{
|
||
reString = "{\"" + jsonName + "\":" + reString + "}";
|
||
}
|
||
return reString;
|
||
}
|
||
|
||
#region 方法 -> DataRow 转换为 JObject
|
||
public static JObject DataRowToJObject(DataRow dr)
|
||
{
|
||
JObject Data = new JObject();
|
||
StringBuilder sb = new StringBuilder();
|
||
sb.Append("{");
|
||
foreach (DataColumn dc in dr.Table.Columns)
|
||
{
|
||
string strKey = dc.ColumnName;
|
||
sb.Append("\"" + strKey + "\":");
|
||
string strValue = dr[dc].ToString();
|
||
Type type = dc.DataType;
|
||
strValue = StringFormat(strValue, type);
|
||
|
||
sb.Append(strValue);
|
||
sb.Append(",");
|
||
}
|
||
sb.Append("}");
|
||
string reString = sb.ToString();
|
||
Data = (JObject)JsonConvert.DeserializeObject(reString);
|
||
return Data;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> DataTable 转换为 JObject
|
||
public static JObject DataTableToJObject(DataTable dt, int PageIndex = 1, int PageSize = 9999, int? TotalCount = null)
|
||
{
|
||
return DataTableToJObject(dt, PageIndex, PageSize, TotalCount, false);
|
||
}
|
||
|
||
public static JObject DataTableToJObject(DataTable dt, int PageIndex, int PageSize, int? TotalCount, bool IsNewFormat)
|
||
{
|
||
JObject Data = new JObject();
|
||
if (IsNewFormat)
|
||
{
|
||
Data.Add("Page_Index", PageIndex); //当前页数
|
||
Data.Add("Page_Size", PageSize); //每页条数
|
||
if (TotalCount != null)
|
||
{
|
||
Data.Add("Total_Count", TotalCount); //记录总数
|
||
}
|
||
else
|
||
{
|
||
Data.Add("Total_Count", dt.Rows.Count); //记录总数
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Data.Add("PageIndex", PageIndex); //当前页数
|
||
Data.Add("PageSize", PageSize); //每页条数
|
||
if (TotalCount != null)
|
||
{
|
||
Data.Add("TotalCount", TotalCount); //记录总数
|
||
}
|
||
else
|
||
{
|
||
Data.Add("TotalCount", dt.Rows.Count); //记录总数
|
||
}
|
||
}
|
||
|
||
StringBuilder sb = new StringBuilder();
|
||
sb.Append("{\"");
|
||
sb.Append("List");
|
||
sb.Append("\":[");
|
||
if (DataTableHelper.IsExistRows(dt))
|
||
{
|
||
foreach (DataRow dr in dt.Rows)
|
||
{
|
||
sb.Append("{");
|
||
foreach (DataColumn dc in dr.Table.Columns)
|
||
{
|
||
sb.Append("\"");
|
||
sb.Append(dc.ColumnName);
|
||
sb.Append("\":");
|
||
//转换成json时保持原有类型
|
||
string strValue = dr[dc].ToString().Replace("\"", "'").Replace("'", "“").Replace(@"\", "/");
|
||
Type type = dc.DataType;
|
||
strValue = StringFormat(strValue, type);
|
||
sb.Append(strValue);
|
||
sb.Append(",");
|
||
}
|
||
sb = sb.Remove(sb.Length - 1, 1);
|
||
sb.Append("},");
|
||
}
|
||
sb = sb.Remove(sb.Length - 1, 1);
|
||
}
|
||
sb.Append("]}");
|
||
|
||
JObject obj = (JObject)JsonConvert.DeserializeObject(sb.ToString());
|
||
//数据列表
|
||
if (IsNewFormat)
|
||
{
|
||
Data["Page_List"] = obj["List"];
|
||
}
|
||
else
|
||
{
|
||
Data["List"] = obj["List"];
|
||
}
|
||
return Data;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> list 转换为 JObject
|
||
public static JObject ListToJObject<T>(List<T> objlist, int PageIndex = 1, int PageSize = 9999, int? TotalCount = null)
|
||
{
|
||
JObject Data = new JObject();
|
||
Data.Add("PageIndex", PageIndex); //当前页数
|
||
Data.Add("PageSize", PageSize); //每页条数
|
||
if (TotalCount != null)
|
||
{
|
||
Data.Add("TotalCount", TotalCount); //记录总数
|
||
}
|
||
else
|
||
{
|
||
Data.Add("TotalCount", objlist.Count); //记录总数
|
||
}
|
||
JObject obj = (JObject)JsonConvert.DeserializeObject(SerializeObject(objlist, "List"));
|
||
Data["List"] = obj["List"];//评价列表
|
||
return Data;
|
||
}
|
||
#endregion
|
||
}
|
||
|
||
}
|