ASP.NET之JSONHelper操作
转自:http://www.cnblogs.com/PEPE/archive/2012/02/13/Pepe_Yu.html
之前说到了Ext.Net中GridPanel行取值的问题(Ext.Net开发_GridPanel行选中取值),涉及到checkBox操作时,要留个心眼注意下取值的区别!返回值是Json格式。 现在用到了Json,就想自己也整一个Josn帮助类。在线帮助的资料很多,在巨人的身上东凑西凑也凑一个用用。
一、介绍
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集
方法有多种,我这边用到是DataContractJsonSerializer类,官方解释:“NET Framework 4 .NET Framework 3.5 Silverlight 将对象序列化为 JavaScript 对象表示法 (JSON),并将 JSON 数据反序列化为对象”。
其命名空间如下:
注意:如果是.NET Framework 3.5 光引用了“System.Runtime.Serialization”了这个还不行。运行时会发现:
所以还要引用下“System.ServiceModel.Web”即可。 .NET Framework 4则在System.Runtime.Serialization下。
二、对JSON数据操作
源码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;
using System.Collections.Generic;
using System.Runtime.Serialization.Json;
namespace WebUI.CommonClass
{
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);
return jsonString;
}
/// <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;
}
}
}
实验下:
Product
public class Product
{
public string proName { get ;set ; }
public decimal proPrice { get; set; }
public string proAttr { get; set; }
public string proAddres { get; set; }
}
操作
1:序列化
Product pro = new Product();
pro.proAddres = "上海";
pro.proAttr = "cpu";
pro.proName = "电脑";
pro.proPrice = 3680;
string josn = JSONHelper.JsonSerializer<Product>(pro); //序列化
Message.ShowAlert("提示", josn);
效果:
2,反序列化
string json = "{\"proAddres\":\"上海\",\"proAttr\":\"cpu\",\"proName\":\"电脑\",\"proPrice\":3680}";
Product pro = JSONHelper.JsonDeserialize<Product>(json);
JSONHELP
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Script.Serialization; using System.Collections; using System.Data; using System.Text; using System.Reflection; using System.Globalization; using System.Text.RegularExpressions; namespace FumaCRM_BS.NewWeb.Common.Class { public class JsonHelper { /// <summary> /// DataTable转Json /// </summary> /// <param name="dtb"></param> /// <returns></returns> public static string Dtb2Json(DataTable dtb) { JavaScriptSerializer jss = new JavaScriptSerializer(); ArrayList dic = new ArrayList(); foreach (DataRow row in dtb.Rows) { Dictionary<string, object> drow = new Dictionary<string, object>(); foreach (DataColumn col in dtb.Columns) { drow.Add(col.ColumnName, row[col.ColumnName]); } dic.Add(drow); } return jss.Serialize(dic); } /// <summary> /// Json转DataTable /// </summary> /// <param name="json"></param> /// <returns></returns> public static DataTable Json2Dtb(string json) { JavaScriptSerializer jss = new JavaScriptSerializer(); ArrayList dic = jss.Deserialize<ArrayList>(json); DataTable dtb = new DataTable(); if (dic.Count > 0) { foreach (Dictionary<string, object> drow in dic) { if (dtb.Columns.Count == 0) { foreach (string key in drow.Keys) { //if (drow[key].GetType() != null) // dtb.Columns.Add(key, drow[key].GetType()); //else dtb.Columns.Add(key); } } DataRow row = dtb.NewRow(); foreach (string key in drow.Keys) { row[key] = drow[key]; } dtb.Rows.Add(row); } } return dtb; } public static string DataTable2Json(DataTable dt) { if (dt == null || dt.Rows.Count == 0) return "[]"; StringBuilder jsonBuilder = new StringBuilder(); jsonBuilder.Append("["); for (int i = 0; i < dt.Rows.Count; i++) { jsonBuilder.Append("{"); for (int j = 0; j < dt.Columns.Count; j++) { jsonBuilder.Append("\""); jsonBuilder.Append(dt.Columns[j].ColumnName); jsonBuilder.Append("\":\""); string value = dt.Rows[i][j].ToString().Replace("\r", "").Replace("\n", "").Replace("\t", "").Trim(); if (dt.Columns[j].DataType.FullName == "System.DateTime") jsonBuilder.Append(value.Length == 0 ? value : Convert.ToDateTime(value).ToString("yyyy-MM-dd")); else jsonBuilder.Append(value); jsonBuilder.Append("\","); } jsonBuilder.Remove(jsonBuilder.Length - 1, 1); jsonBuilder.Append("},"); } jsonBuilder.Remove(jsonBuilder.Length - 1, 1); jsonBuilder.Append("]"); return jsonBuilder.ToString(); } #region JqueryEasyUI JSON Method /// <summary> /// 生成表单编辑赋值 JSON格式 /// </summary> /// <param name="dt"></param> /// <param name="displayCount"></param> /// <returns></returns> public static string CreateJsonOne(DataTable dt, bool displayCount) { StringBuilder JsonString = new StringBuilder(); //Exception Handling if (dt != null && dt.Rows.Count > 0) { for (int i = 0; i < dt.Rows.Count; i++) { JsonString.Append("{ "); for (int j = 0; j < dt.Columns.Count; j++) { if (j < dt.Columns.Count - 1) { JsonString.Append("ipt_" + dt.Columns[j].ColumnName.ToString().ToLower() + ":" + "\"" + dt.Rows[i][j].ToString() + "\","); } else if (j == dt.Columns.Count - 1) { JsonString.Append("ipt_" + dt.Columns[j].ColumnName.ToString().ToLower() + ":" + "\"" + dt.Rows[i][j].ToString() + "\""); } } if (i == dt.Rows.Count - 1) { JsonString.Append("} "); } else { JsonString.Append("}, "); } } return JsonString.ToString(); } else { return null; } } /// <summary> /// 将DataTable中的数据转换成JSON格式 /// </summary> /// <param name="dt">数据源DataTable</param> /// <param name="displayCount">是否输出数据总条数</param> /// <returns></returns> public static string CreateJsonParameters(DataTable dt, bool displayCount) { return CreateJsonParameters(dt, displayCount, dt.Rows.Count); } /// <summary> /// 将DataTable中的数据转换成JSON格式 /// </summary> /// <param name="dt">数据源DataTable</param> /// <returns></returns> public static string CreateJsonParameters(DataTable dt) { return CreateJsonParameters(dt, true); } /// <summary> /// 将DataTable中的数据转换成JSON格式 /// </summary> /// <param name="dt">数据源DataTable</param> /// <param name="displayCount">是否输出数据总条数</param> /// <param name="totalcount">JSON中显示的数据总条数</param> /// <returns></returns> public static string CreateJsonParameters(DataTable dt, bool displayCount, int totalcount) { StringBuilder JsonString = new StringBuilder(); //Exception Handling if (dt != null) { JsonString.Append("{ "); JsonString.Append("\"rows\":[ "); for (int i = 0; i < dt.Rows.Count; i++) { JsonString.Append("{ "); for (int j = 0; j < dt.Columns.Count; j++) { if (j < dt.Columns.Count - 1) { //if (dt.Rows[i][j] == DBNull.Value) continue; if (dt.Columns[j].DataType == typeof(bool)) { JsonString.Append("\"JSON_" + dt.Columns[j].ColumnName.ToLower() + "\":" + dt.Rows[i][j].ToString().ToLower() + ","); } else if (dt.Columns[j].DataType == typeof(string)) { JsonString.Append("\"JSON_" + dt.Columns[j].ColumnName.ToLower() + "\":" + "\"" + dt.Rows[i][j].ToString().Replace("\"", "\\\"") + "\","); } else { JsonString.Append("\"JSON_" + dt.Columns[j].ColumnName.ToLower() + "\":" + "\"" + dt.Rows[i][j] + "\","); } } else if (j == dt.Columns.Count - 1) { //if (dt.Rows[i][j] == DBNull.Value) continue; if (dt.Columns[j].DataType == typeof(bool)) { JsonString.Append("\"JSON_" + dt.Columns[j].ColumnName.ToLower() + "\":" + dt.Rows[i][j].ToString().ToLower()); } else if (dt.Columns[j].DataType == typeof(string)) { JsonString.Append("\"JSON_" + dt.Columns[j].ColumnName.ToLower() + "\":" + "\"" + dt.Rows[i][j].ToString().Replace("\"", "\\\"") + "\""); } else { JsonString.Append("\"JSON_" + dt.Columns[j].ColumnName.ToLower() + "\":" + "\"" + dt.Rows[i][j] + "\""); } } } /*end Of String*/ if (i == dt.Rows.Count - 1) { JsonString.Append("} "); } else { JsonString.Append("}, "); } } JsonString.Append("]"); if (displayCount) { JsonString.Append(","); JsonString.Append("\"total\":"); JsonString.Append(totalcount); } JsonString.Append("}"); return JsonString.ToString().Replace("\n", ""); } else { return null; } } #region object 2 json private static void WriteDataRow(StringBuilder sb, DataRow row) { sb.Append("{"); foreach (DataColumn column in row.Table.Columns) { sb.AppendFormat("\"{0}\":", column.ColumnName); WriteValue(sb, row[column]); sb.Append(","); } // Remove the trailing comma. if (row.Table.Columns.Count > 0) { --sb.Length; } sb.Append("}"); } private static void WriteDataSet(StringBuilder sb, DataSet ds) { sb.Append("{\"Tables\":{"); foreach (DataTable table in ds.Tables) { sb.AppendFormat("\"{0}\":", table.TableName); WriteDataTable(sb, table); sb.Append(","); } // Remove the trailing comma. if (ds.Tables.Count > 0) { --sb.Length; } sb.Append("}}"); } private static void WriteDataTable(StringBuilder sb, DataTable table) { sb.Append("{\"Rows\":["); foreach (DataRow row in table.Rows) { WriteDataRow(sb, row); sb.Append(","); } // Remove the trailing comma. if (table.Rows.Count > 0) { --sb.Length; } sb.Append("]}"); } private static void WriteEnumerable(StringBuilder sb, IEnumerable e) { bool hasItems = false; sb.Append("["); foreach (object val in e) { WriteValue(sb, val); sb.Append(","); hasItems = true; } // Remove the trailing comma. if (hasItems) { --sb.Length; } sb.Append("]"); } private static void WriteHashtable(StringBuilder sb, IDictionary e) { bool hasItems = false; sb.Append("{"); foreach (string key in e.Keys) { sb.AppendFormat("\"{0}\":", key.ToLower()); WriteValue(sb, e[key]); sb.Append(","); hasItems = true; } // Remove the trailing comma. if (hasItems) { --sb.Length; } sb.Append("}"); } private static void WriteObject(StringBuilder sb, object o) { MemberInfo[] members = o.GetType().GetMembers(BindingFlags.Instance | BindingFlags.Public); sb.Append("{"); bool hasMembers = false; foreach (MemberInfo member in members) { bool hasValue = false; object val = null; if ((member.MemberType & MemberTypes.Field) == MemberTypes.Field) { FieldInfo field = (FieldInfo)member; val = field.GetValue(o); hasValue = true; } else if ((member.MemberType & MemberTypes.Property) == MemberTypes.Property) { PropertyInfo property = (PropertyInfo)member; if (property.CanRead && property.GetIndexParameters().Length == 0) { val = property.GetValue(o, null); hasValue = true; } } if (hasValue) { sb.Append("\""); sb.Append(member.Name); sb.Append("\":"); WriteValue(sb, val); sb.Append(","); hasMembers = true; } } if (hasMembers) { --sb.Length; } sb.Append("}"); } private static void WriteString(StringBuilder sb, IEnumerable s) { sb.Append("\""); foreach (char c in s) { switch (c) { 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; default: int i = c; if (i < 32 || i > 127) { sb.AppendFormat("\\u{0:X04}", i); } else { sb.Append(c); } break; } } sb.Append("\""); } public static void WriteValue(StringBuilder sb, object val) { if (val == null || val == DBNull.Value) { sb.Append("null"); } else if (val is string || val is Guid) { WriteString(sb, val.ToString()); } else if (val is bool) { sb.Append(val.ToString().ToLower()); } else if (val is double || val is float || val is long || val is int || val is short || val is byte || val is decimal) { sb.AppendFormat(CultureInfo.InvariantCulture.NumberFormat, "{0}", val); } else if (val.GetType().IsEnum) { sb.Append((int)val); } else if (val is DateTime) { sb.Append("new Date(\""); sb.Append(((DateTime)val).ToString("MMMM, d yyyy HH:mm:ss", new CultureInfo("en-US", false).DateTimeFormat)); sb.Append("\")"); } else if (val is DataSet) { WriteDataSet(sb, val as DataSet); } else if (val is DataTable) { WriteDataTable(sb, val as DataTable); } else if (val is DataRow) { WriteDataRow(sb, val as DataRow); } else if (val is Hashtable) { WriteHashtable(sb, val as Hashtable); } else if (val is IEnumerable) { WriteEnumerable(sb, val as IEnumerable); } else { WriteObject(sb, val); } } /// <summary> /// /// </summary> /// <param name="o"></param> /// <returns></returns> public static string Convert2Json(object o) { StringBuilder sb = new StringBuilder(); WriteValue(sb, o); return sb.ToString(); } #endregion #endregion /// <summary> /// 转换对象为JSON格式数据 /// </summary> /// <typeparam name="T">类</typeparam> /// <param name="obj">对象</param> /// <returns>字符格式的JSON数据</returns> public static string GetJSON<T>(object obj) { string result = String.Empty; try { System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T)); using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { serializer.WriteObject(ms, obj); result = System.Text.Encoding.UTF8.GetString(ms.ToArray()); } } catch (Exception ex) { throw ex; } return result; } /// <summary> /// 转换List<T>的数据为JSON格式 /// </summary> /// <typeparam name="T">类</typeparam> /// <param name="vals">列表值</param> /// <returns>JSON格式数据</returns> public static string JSON<T>(List<T> vals) { System.Text.StringBuilder st = new System.Text.StringBuilder(); try { System.Runtime.Serialization.Json.DataContractJsonSerializer s = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T)); foreach (T city in vals) { using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { s.WriteObject(ms, city); st.Append(System.Text.Encoding.UTF8.GetString(ms.ToArray())); } } } catch (Exception ex) { throw ex; } return st.ToString(); } /// <summary> /// JSON格式字符转换为T类型的对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="jsonStr"></param> /// <returns></returns> public static T ParseFormByJson<T>(string jsonStr) { T obj = Activator.CreateInstance<T>(); using (System.IO.MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(jsonStr))) { System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T)); return (T)serializer.ReadObject(ms); } } /// <summary> /// 将获取的Json数据转换为DataTable /// </summary> /// <param name="strJson">Json字符串</param> /// <returns></returns> public static DataTable JsonToDataTable(string strJson) { //取出表名 var rg = new Regex(@"(?<={)[^:]+(?=:\[)", RegexOptions.IgnoreCase); string strName = rg.Match(strJson).Value; DataTable tb = null; //去除表名 strJson = strJson.Substring(strJson.IndexOf("[") + 1); strJson = strJson.Substring(0, strJson.IndexOf("]")); //获取数据 rg = new Regex(@"(?<={)[^}]+(?=})"); MatchCollection mc = rg.Matches(strJson); for (int i = 0; i < mc.Count; i++) { string strRow = mc[i].Value; string[] strRows = strRow.Split(‘,‘); //创建表 if (tb == null) { tb = new DataTable(); tb.TableName = strName; foreach (string str in strRows) { var dc = new DataColumn(); string[] strCell = str.Split(‘:‘); dc.ColumnName = strCell[0].Replace("\"", "");// strCell[0]; tb.Columns.Add(dc); } tb.AcceptChanges(); } //增加内容 DataRow dr = tb.NewRow(); for (int r = 0; r < strRows.Length; r++) { dr[r] = strRows[r].Split(‘:‘)[1].Trim().Replace(",", ",").Replace(":", ":").Replace("\"", ""); } tb.Rows.Add(dr); tb.AcceptChanges(); } return tb; } /// <summary> /// /// </summary> /// <param name="strJson"></param> /// <param name="symbol"></param> /// <returns></returns> public static DataTable JsonToDataTable(string strJson, string symbol) { //取出表名 var rg = new Regex(@"(?<={)[^:]+(?=:\[)", RegexOptions.IgnoreCase); string strName = rg.Match(strJson).Value; DataTable tb = null; //去除表名 strJson = strJson.Substring(strJson.IndexOf("[") + 1); strJson = strJson.Substring(0, strJson.IndexOf("]")); //获取数据 rg = new Regex(@"(?<={)[^}]+(?=})"); MatchCollection mc = rg.Matches(strJson); for (int i = 0; i < mc.Count; i++) { string strRow = mc[i].Value; string[] strRows = strRow.Replace(symbol, "|").Split(‘|‘);// (symbol); //创建表 if (tb == null) { tb = new DataTable(); tb.TableName = strName; foreach (string str in strRows) { var dc = new DataColumn(); string[] strCell = str.Split(‘:‘); dc.ColumnName = strCell[0].Replace("\"", "");// strCell[0]; tb.Columns.Add(dc); } tb.AcceptChanges(); } //增加内容 DataRow dr = tb.NewRow(); for (int r = 0; r < strRows.Length; r++) { dr[r] = strRows[r].Split(‘:‘)[1].Trim().Replace(",", ",").Replace(":", ":").Replace("\"", ""); } tb.Rows.Add(dr); tb.AcceptChanges(); } return tb; } /// <summary> /// dataTable转换成Json格式 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string ToJson(DataTable dt) { StringBuilder jsonBuilder = new StringBuilder(); jsonBuilder.Append("{\""); jsonBuilder.Append(dt.TableName); jsonBuilder.Append("\":["); for (int i = 0; i < dt.Rows.Count; i++) { jsonBuilder.Append("{"); for (int j = 0; j < dt.Columns.Count; j++) { jsonBuilder.Append("\""); jsonBuilder.Append(dt.Columns[j].ColumnName); jsonBuilder.Append("\":\""); jsonBuilder.Append(dt.Rows[i][j].ToString()); jsonBuilder.Append("\","); } jsonBuilder.Remove(jsonBuilder.Length - 1, 1); jsonBuilder.Append("},"); } jsonBuilder.Remove(jsonBuilder.Length - 1, 1); jsonBuilder.Append("]"); jsonBuilder.Append("}"); return jsonBuilder.ToString(); } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。