DataTable、Model转json

1 public static string DateFormatString { get; set; }
2 public static string DefaultJson = "{}";

 

1、转义html

 1 /// <summary>
 2 /// 转义html
 3 /// </summary>
 4 /// <param name="htmlStr"></param>
 5 public static string ConvertHtml(string htmlStr)
 6 {
 7 if ((htmlStr + "").Length > 0)
 8 {
 9 return htmlStr.Replace("\\\"", "\"")
10 .Replace("\"", "\\\"")
11 .Replace("\\", "\\\\")
12 .Replace("\r", "<br/>")
13 .Replace("\n", "<br/>")
14 .Replace("\t", "")
15 .Trim();
16 }
17 return "";
18 }
View Code
 1 public static string TrimStr(object str)
 2 {
 3 if (str == null) return "";
 4 switch (str.GetType().Name)
 5 {
 6 case "Int32":
 7 return str.ToString();
 8 break;
 9 case "DateTime":
10 if (string.IsNullOrEmpty(DateFormatString))
11 DateFormatString = "yyyy-MM-dd HH:mm:ss";
12 return (str is DateTime ? (DateTime)str : new DateTime()).ToString(DateFormatString);
13 break;
14 case "String":
15 return (str + "").Length > 0 ? str.ToString() : "";
16 break;
17 default:
18 return str.ToString();
19 break;
20 }
21 }
View Code

2、封装json字符串

 1 /// <summary>
 2 /// 封装json字符串
 3 /// </summary>
 4 /// <param name="fields">字段数组</param>
 5 /// <param name="values">值数组</param>
 6 /// <returns></returns>
 7 public static string ArrayToJson(string[] fields, string[] values, bool isConvent = true)
 8 {
 9 var jsonStringBuilder = new StringBuilder("{");
10 int i = 0;
11 if (fields.Length > 0 && fields.Length <= values.Length)
12 {
13 if (isConvent)
14 {
15 foreach (string value in values)
16 {
17 jsonStringBuilder.Append("\"" + fields[i++] + "\":\"" + ConvertHtml(TrimStr(value.Trim())) +
18 "\",");
19 }
20 }
21 else
22 {
23 foreach (string value in values)
24 {
25 jsonStringBuilder.Append("\"" + fields[i++] + "\":" + TrimStr(value.Trim()) +
26 ",");
27 }
28 
29 }
30 jsonStringBuilder = jsonStringBuilder.Remove(jsonStringBuilder.Length - 1, 1);
31 }
32 jsonStringBuilder.Append("}");
33 return jsonStringBuilder.ToString();
34 }
View Code

3、DataTable二维表格转为符合json格式的字符串

 1 /// <summary> DataTable二维表格转为符合json格式的字符串 </summary>
 2 /// <param name="tableSource">数据库查询结果</param>
 3 /// <param name="gnorgColumn">过滤列</param>
 4 /// <param name="isChilid">是否是子节点</param>
 5 /// <returns></returns>
 6 public static string DataTableToJsonByIgnorg(DataTable tableSource, string[] gnorgColumn = null,
 7 bool isChilid = false)
 8 {
 9 var columnStrings = new List<string>();
10 foreach (DataColumn column in tableSource.Columns)
11 {
12 if (gnorgColumn != null && gnorgColumn.Contains(column.ColumnName))
13 {
14 continue;
15 }
16 columnStrings.Add(column.ColumnName);
17 }
18 return DataTableToJson(tableSource, columnStrings.ToArray(), isChilid);
19 }
View Code

4、DataTable二维表格转为符合json格式的字符串

 1 /// <summary> DataTable二维表格转为符合json格式的字符串 </summary>
 2 /// <param name="tableSource">数据库查询结果</param>
 3 /// <param name="fields">需要添加进来的字段名</param>
 4 /// <param name="isChild">是否是子节点</param>
 5 /// <returns></returns>
 6 public static string DataTableToJson(DataTable tableSource, string[] fields, bool isChild = false)
 7 {
 8 var jsonData = new StringBuilder("{\"totalCount\":" + tableSource.Rows.Count + ",\"items\":[");
 9 if (isChild)
10 jsonData = new StringBuilder("[");
11 if (tableSource.Rows.Count > 0)
12 {
13 foreach (DataRow row in tableSource.Rows)
14 {
15 jsonData.Append("{");
16 jsonData = fields.Aggregate(jsonData,
17 (current, t) => current.Append(("\"" + t + "\":\"" + ConvertHtml(TrimStr(row[t])) + "\",")));
18 jsonData = jsonData.Remove(jsonData.Length - 1, 1);
19 jsonData.Append("},");
20 }
21 jsonData = jsonData.Remove(jsonData.Length - 1, 1);
22 }
23 jsonData.Append(isChild ? "]" : "]}");
24 
25 
26 return jsonData.ToString();
27 }
28 
29 public static string ConvertToJson<T>(T t) where T : class
30 {
31 if (t == null) return "{}";
32 
33 var jsonData = new StringBuilder();
34 jsonData.Append("{");
35 PropertyInfo[] propertys = t.GetType().GetProperties();
36 
37 foreach (PropertyInfo pi in propertys)
38 {
39 jsonData.Append(("\"" + pi.Name + "\":\"" +
40 ConvertHtml(TrimStr(pi.GetValue(t, null))) + "\","));
41 }
42 jsonData = jsonData.Remove(jsonData.Length - 1, 1);
43 jsonData.Append("}");
44 
45 return jsonData.ToString();
46 }
View Code

5、model列表转化为json

 1 /// <summary>
 2 /// model列表转化为json
 3 /// </summary>
 4 /// <param name="ts">model列表</param>
 5 /// <param name="isChild">是否是子节点</param>
 6 /// <returns></returns>
 7 public static string ConvertToJson<T>(IList<T> ts, bool isChild = false) where T : class
 8 {
 9 if (ts == null) return DefaultJson;
10 
11 var jsonData = new StringBuilder("{\"totalCount\":" + ts.Count + ",\"items\":[");
12 if (isChild)
13 jsonData = new StringBuilder("[");
14 foreach (T t in ts)
15 {
16 jsonData.Append("{");
17 PropertyInfo[] propertys = t.GetType().GetProperties();
18 
19 foreach (PropertyInfo pi in propertys)
20 {
21 jsonData.Append(("\"" + pi.Name + "\":\"" +
22 ConvertHtml(TrimStr(pi.GetValue(t, null))) +
23 "\","));
24 }
25 jsonData = jsonData.Remove(jsonData.Length - 1, 1);
26 jsonData.Append("},");
27 }
28 if (ts.Count > 0)
29 jsonData = jsonData.Remove(jsonData.Length - 1, 1);
30 jsonData.Append(isChild ? "]" : "]}");
31 return jsonData.ToString();
32 }
33 
34 /// <summary>
35 /// model列表转化为json
36 /// </summary>
37 /// <param name="ts">model列表</param>
38 /// <param name="isHasChild">是否有子节点</param>
39 /// <returns></returns>
40 public static string ConvertToJson<T>(IList<T> ts, int isHasChild) where T : class
41 {
42 if (ts == null) return DefaultJson;
43 
44 var jsonData = new StringBuilder("{\"totalCount\":" + ts.Count + ",\"items\":");
45 jsonData.Append(isHasChild == 1 ? GetJson(ts) : "[]");
46 jsonData.Append("}");
47 return jsonData.ToString();
48 }
49 
50 /// <summary>
51 /// model列表转化为json
52 /// </summary>
53 /// <param name="ts">model列表</param>
54 /// <param name="isHasChild">是否有子节点</param>
55 /// <returns></returns>
56 public static string ConvertToJson<T>(T ts, int isHasChild) where T : class
57 {
58 if (ts == null) return DefaultJson;
59 
60 var jsonData = new StringBuilder("{\"totalCount\":1,\"items\":");
61 jsonData.Append(isHasChild == 1 ? GetJson(ts) : "[]");
62 jsonData.Append("}");
63 return jsonData.ToString();
64 }
View Code

6、反序历❀JSON

 1 public static string GetJson(object obj)
 2 {
 3 string str;
 4 try
 5 {
 6 var js = new JavaScriptSerializer { MaxJsonLength = int.MaxValue };
 7 str = js.Serialize(obj);
 8 }
 9 catch
10 {
11 str = "";
12 }
13 return str;
14 }
View Code

 

posted @ 2017-07-18 13:48  D调灬仔  阅读(372)  评论(0编辑  收藏  举报

作者D调灬仔

出处:https://www.cnblogs.com/chj929555796/

如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!