306 lines
12 KiB
C#
306 lines
12 KiB
C#
/******************************
|
|
* 说明:Json相关操作
|
|
* 创建人:龚宇超
|
|
* 创建日期:2017-10-23
|
|
* 修改人:
|
|
* 修改日期:
|
|
* 修改备注:
|
|
* 版本:1.0.0.0
|
|
******************************/
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Data;
|
|
using System.Web.Script.Serialization;
|
|
using System.Collections;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace Lskj.Util
|
|
{
|
|
/// <summary>
|
|
/// Json 相关操作
|
|
/// </summary>
|
|
public static class JsonUtil
|
|
{
|
|
/// <summary>
|
|
/// Json 字符串 转换为 DataTable数据集合
|
|
/// </summary>
|
|
/// <param name="json"></param>
|
|
/// <returns></returns>
|
|
public static DataTable ToComplexTable(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;
|
|
}
|
|
//Columns
|
|
if (dataTable.Columns.Count == 0)
|
|
{
|
|
foreach (string current in dictionary.Keys)
|
|
{
|
|
if (current != "data")
|
|
dataTable.Columns.Add(current, dictionary[current].GetType());
|
|
else
|
|
{
|
|
ArrayList list = dictionary[current] as ArrayList;
|
|
foreach (Dictionary<string, object> dic in list)
|
|
{
|
|
foreach (string key in dic.Keys)
|
|
{
|
|
dataTable.Columns.Add(key, dic[key].GetType());
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
//Rows
|
|
string root = "";
|
|
foreach (string current in dictionary.Keys)
|
|
{
|
|
if (current != "data")
|
|
root = current;
|
|
else
|
|
{
|
|
ArrayList list = dictionary[current] as ArrayList;
|
|
foreach (Dictionary<string, object> dic in list)
|
|
{
|
|
DataRow dataRow = dataTable.NewRow();
|
|
dataRow[root] = dictionary[root];
|
|
foreach (string key in dic.Keys)
|
|
{
|
|
dataRow[key] = dic[key];
|
|
}
|
|
dataTable.Rows.Add(dataRow);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
result = dataTable;
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:json 字符串转换为DataTable</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-10-23 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="json">The json.</param>
|
|
/// <returns>DataTable.</returns>
|
|
public static DataTable ToDataTable(this string json)
|
|
{
|
|
int interception = json.LastIndexOf(']');
|
|
json = json.Substring(0, interception + 1);
|
|
DataTable dataTable = new DataTable(); //实例化
|
|
DataTable result;
|
|
try
|
|
{
|
|
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
|
|
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
|
|
ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
|
|
if (arrayList != null && arrayList.Count > 0)
|
|
{
|
|
foreach (Dictionary<string, object> dictionary in arrayList)
|
|
{
|
|
if (dictionary.Keys.Count<string>() == 0)
|
|
{
|
|
result = dataTable;
|
|
return result;
|
|
}
|
|
if (dataTable.Columns.Count == 0)
|
|
{
|
|
foreach (string current in dictionary.Keys)
|
|
{
|
|
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 (Exception ex)
|
|
{
|
|
|
|
}
|
|
result = dataTable;
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:将可能包含对象或数组字段的网页行 JSON 转换为 DataTable</para>
|
|
/// <para>复杂字段所在列统一保留为字符串,纯标量列保持现有类型推断</para>
|
|
/// </summary>
|
|
/// <param name="json">顶层对象或对象数组 JSON</param>
|
|
/// <returns>DataTable.</returns>
|
|
public static DataTable ToDataTablePreservingComplexValues(string json)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(json))
|
|
{
|
|
throw new ArgumentException("网页行 JSON 不能为空。", "json");
|
|
}
|
|
|
|
JToken root = JToken.Parse(json);
|
|
JArray rows;
|
|
if (root.Type == JTokenType.Array)
|
|
{
|
|
rows = (JArray)root;
|
|
}
|
|
else if (root.Type == JTokenType.Object)
|
|
{
|
|
rows = new JArray(root);
|
|
}
|
|
else
|
|
{
|
|
throw new JsonSerializationException("网页行 JSON 的顶层必须是对象或对象数组。");
|
|
}
|
|
|
|
HashSet<string> complexColumns = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
|
foreach (JToken rowToken in rows)
|
|
{
|
|
if (rowToken.Type != JTokenType.Object)
|
|
{
|
|
throw new JsonSerializationException("网页行 JSON 数组中的每一项都必须是对象。");
|
|
}
|
|
|
|
foreach (JProperty property in ((JObject)rowToken).Properties())
|
|
{
|
|
if (property.Value.Type == JTokenType.Object || property.Value.Type == JTokenType.Array)
|
|
{
|
|
complexColumns.Add(property.Name);
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (JObject row in rows.Children<JObject>())
|
|
{
|
|
foreach (JProperty property in row.Properties().ToList())
|
|
{
|
|
if (!complexColumns.Contains(property.Name)
|
|
|| property.Value.Type == JTokenType.Null
|
|
|| property.Value.Type == JTokenType.Undefined)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
string text;
|
|
if (property.Value.Type == JTokenType.Object || property.Value.Type == JTokenType.Array)
|
|
{
|
|
text = property.Value.ToString(Formatting.None);
|
|
}
|
|
else if (property.Value.Type == JTokenType.String)
|
|
{
|
|
text = property.Value.Value<string>();
|
|
}
|
|
else
|
|
{
|
|
JValue scalar = property.Value as JValue;
|
|
text = scalar == null || scalar.Value == null
|
|
? null
|
|
: Convert.ToString(scalar.Value, CultureInfo.InvariantCulture);
|
|
}
|
|
|
|
property.Value = text == null ? JValue.CreateNull() : new JValue(text);
|
|
}
|
|
}
|
|
|
|
return JsonConvert.DeserializeObject<DataTable>(rows.ToString(Formatting.None));
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-10-23 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="json">The json.</param>
|
|
/// <returns>DataRow.</returns>
|
|
public static DataRow ToDataRow(this string json)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(json)) return null;
|
|
JObject rowObject = JsonConvert.DeserializeObject<JObject>(json);
|
|
JArray jArray = new JArray();
|
|
jArray.Add(rowObject);
|
|
DataTable table = JsonConvert.DeserializeObject<DataTable>(JsonConvert.SerializeObject(jArray));
|
|
return table != null && table.Rows.Count > 0 ? table.Rows[0] : null;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:DataTable To JsonArray</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-10-23 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="dtTable">The dt table.</param>
|
|
/// <returns>System.String.</returns>
|
|
public static string ToJsonArray(this DataTable dtTable)
|
|
{
|
|
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
|
|
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
|
|
ArrayList arrayList = new ArrayList();
|
|
foreach (DataRow dataRow in dtTable.Rows)
|
|
{
|
|
Dictionary<string, object> dictionary = new Dictionary<string, object>(); //实例化一个参数集合
|
|
foreach (DataColumn dataColumn in dtTable.Columns)
|
|
{
|
|
dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName].ToStr());
|
|
}
|
|
arrayList.Add(dictionary); //ArrayList集合中添加键值
|
|
}
|
|
|
|
return javaScriptSerializer.Serialize(arrayList); //返回一个json字符串
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:DataRow To Json</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-10-23 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="row">The row.</param>
|
|
/// <returns>System.String.</returns>
|
|
public static string ToJsonObject(this DataRow row)
|
|
{
|
|
DataTable dataTable = row.Table.Clone();
|
|
DataRow dataRow = dataTable.NewRow();
|
|
dataRow.ItemArray = row.ItemArray;
|
|
dataTable.Rows.Add(dataRow);
|
|
JArray jArray = JsonConvert.DeserializeObject<JArray>(JsonConvert.SerializeObject(dataTable));
|
|
return jArray[0] + "";
|
|
}
|
|
}
|
|
}
|