123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- using Newtonsoft.Json;
- using Newtonsoft.Json.Converters;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Text;
- using System.Web.Script.Serialization;
- namespace HNWD.Pregrant.Common
- {
- public static class JsonHelper
- {
- public static string Serialize(Object o, string str2)
- {
- JavaScriptSerializer Serializer = new JavaScriptSerializer();
- Serializer.MaxJsonLength = Int32.MaxValue;
- string str = Serializer.Serialize(o);
- return str;
- }
- public static string Serialize(Object o)
- {
- JavaScriptSerializer Serializer = new JavaScriptSerializer();
- Serializer.MaxJsonLength = Int32.MaxValue;
- string str = Serializer.Serialize(o);
- return str;
- }
- public static string SerializeWithSuffix(Object o, string Suffix = ",OK!")
- {
- JavaScriptSerializer Serializer = new JavaScriptSerializer();
- Serializer.MaxJsonLength = int.MaxValue;
- string str = Serializer.Serialize(o);
- return str + Suffix;
- }
- public static string Serialize(DataSet o)
- {
- string str = JsonConvert.SerializeObject(o, new DataTableConverter());
- str = str.Replace("Table", "dataList");
- return str;
- }
- public static string Serialize(DataTable table, string type = "javascript")
- {
- JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
- List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
- Dictionary<string, object> childRow;
- foreach (DataRow row in table.Rows)
- {
- childRow = new Dictionary<string, object>();
- foreach (DataColumn col in table.Columns)
- {
- childRow.Add(col.ColumnName, row[col]);
- }
- parentRow.Add(childRow);
- }
- return jsSerializer.Serialize(parentRow);
- }
- public static string Serialize(DataTable table, string type = "net", string type2 = "net")
- {
- string jsonString = JsonConvert.SerializeObject(table);
- return jsonString;
- }
- public static string Serialize(DataTable table, string type = "string", string type2 = "string", string type3 = "string")
- {
- var jsonString = new StringBuilder();
- jsonString.Append("{\"" + table.TableName + "\":");
- if (table.Rows.Count > 0)
- {
- jsonString.Append("[");
- for (int i = 0; i < table.Rows.Count; i++)
- {
- jsonString.Append("{");
- for (int j = 0; j < table.Columns.Count; j++)
- {
- if (j < table.Columns.Count - 1)
- {
- jsonString.Append("\"" + table.Columns[j].ColumnName.ToString()
- + "\":" + "\""
- + table.Rows[i][j].ToString() + "\",");
- }
- else if (j == table.Columns.Count - 1)
- {
- jsonString.Append("\"" + table.Columns[j].ColumnName.ToString()
- + "\":" + "\""
- + table.Rows[i][j].ToString() + "\"");
- }
- }
- if (i == table.Rows.Count - 1)
- {
- jsonString.Append("}");
- }
- else
- {
- jsonString.Append("},");
- }
- }
- jsonString.Append("]");
- jsonString.Append("}");
- }
- return jsonString.ToString();
- }
- public static T Deserialize<T>(string json)
- {
- JavaScriptSerializer Serializer = new JavaScriptSerializer();
- T objs = Serializer.Deserialize<T>(json);
- return objs;
- }
- public static T DeserializeWithSuffix<T>(string json, string Suffix = ",OK!")
- {
- json = json.Replace(Suffix, "");
- JavaScriptSerializer Serializer = new JavaScriptSerializer();
- T objs = Serializer.Deserialize<T>(json);
- return objs;
- }
- }
- }
|