JsonHelper.cs
6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// ***********************************************************************
// <summary>json序列化帮助类</summary>
// ***********************************************************************
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
namespace Infrastructure
{
public class JsonHelper
{
private static JsonHelper _jsonHelper = new JsonHelper();
public static JsonHelper Instance { get { return _jsonHelper; } }
public string Serialize(object obj)
{
return JsonConvert.SerializeObject(obj, new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" });
}
public string SerializeByConverter(object obj, params JsonConverter[] converters)
{
return JsonConvert.SerializeObject(obj, converters);
}
public T Deserialize<T>(string input)
{
return JsonConvert.DeserializeObject<T>(input);
}
public T DeserializeByConverter<T>(string input,params JsonConverter[] converter)
{
return JsonConvert.DeserializeObject<T>(input, converter);
}
public T DeserializeBySetting<T>(string input, JsonSerializerSettings settings)
{
return JsonConvert.DeserializeObject<T>(input, settings);
}
private object NullToEmpty(object obj)
{
return null;
}
public static string GetDataTableForJson(DataTable dt)
{
StringBuilder json = new StringBuilder();
if (dt == null || dt.Rows.Count == 0) return json.Append("]}").ToString();
for (int i = 0; i < dt.Rows.Count; i++)
{
json.Append("{");
for (int j = 0; j < dt.Columns.Count; j++)
{
json.Append(string.Format("\"{0}\":\"{1}\",", dt.Columns[j].ColumnName, dt.Rows[i][j].ToString()));
}
json.Remove(json.ToString().LastIndexOf(","), 1);
json.Append("}");
json.Append(",");
}
json.Remove(json.ToString().LastIndexOf(","), 1);
return json.ToString();
//StringBuilder json = new StringBuilder();
//json.Append("{\"count\":" + dt.Rows.Count.ToString());
//json.Append(",");
//json.Append("\"data\":[");
//if (dt == null || dt.Rows.Count == 0) return json.Append("]}").ToString();
//for (int i = 0; i < dt.Rows.Count; i++)
//{
// json.Append("{");
// for (int j = 0; j < dt.Columns.Count; j++)
// {
// json.Append(string.Format("\"{0}\":\"{1}\",", dt.Columns[j].ColumnName, dt.Rows[i][j].ToString()));
// }
// json.Remove(json.ToString().LastIndexOf(","), 1);
// json.Append("}");
// json.Append(",");
//}
//json.Remove(json.ToString().LastIndexOf(","), 1);
//json.Append("]}");
//return json.ToString();
}
public static DataTable JsonConvertToDataTable(string strJson)
{
//转换json格式
string splitChar0 = "*";
string splitChar0_1 = "splitChar0";
string splitChar1 = "#";
string splitChar1_1 = "splitChar1";
strJson = strJson.Replace(splitChar0, splitChar0_1).Replace(splitChar1, splitChar1_1);
strJson = strJson.Replace(",\"", "*\"").Replace("\":", "\"#").ToString();
//取出表名
var rg = new Regex(@"(?<={)[^:]+(?=:\[)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
string strName = rg.Match(strJson).Value.Replace(splitChar0_1, splitChar0).Replace(splitChar1_1, splitChar1);
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();
if (str.Contains("#"))
{
string[] strCell = str.Split("#");
if (strCell[0].Substring(0, 1) == "\"")
{
int a = strCell[0].Length;
if (a > 2)
{
dc.ColumnName = strCell[0].Substring(1, a - 2).Replace(splitChar0_1, splitChar0).Replace(splitChar1_1, splitChar1);
}
}
else
{
dc.ColumnName = strCell[0].Replace(splitChar0_1, splitChar0).Replace(splitChar1_1, splitChar1);
}
tb.Columns.Add(dc);
}
}
tb.AcceptChanges();
}
//增加内容
DataRow dr = tb.NewRow();
int k = 0;
for (int r = 0; r < strRows.Length; r++)
{
if (strRows[r].Contains("#"))
{
string svalue = strRows[r].Split("#")[1].Trim().Replace(",", ",").Replace(":", ":").Replace("\"", "").Replace(splitChar0_1, splitChar0).Replace(splitChar1_1, splitChar1);
if (svalue != "null")
{
dr[k] = svalue;
}
}
else
{
k = r - 1;
}
k++;
}
tb.Rows.Add(dr);
tb.AcceptChanges();
}
return tb;
}
}
}