-
C#教程之C#中的DataSet、string、DataTable、对象转换成
C#中对象,字符串,dataTable、DataReader、DataSet,对象集合转换成Json字符串方法。
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
public class ConvertJson { #region 私有方法 /// <summary> /// 过滤特殊字符 /// </summary> /// <param name="s">字符串</param> /// <returns>json字符串</returns> private static string String2Json(String s) { StringBuilder sb = new StringBuilder(); for ( int i = 0; i < s.Length; i++) { char c = s.ToCharArray()[i]; switch (c) { case '\"' : sb.Append( "\\\"" ); break ; 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 : sb.Append(c); break ; } } return sb.ToString(); } /// <summary> /// 格式化字符型、日期型、布尔型 /// </summary> /// <param name="str"></param> /// <param name="type"></param> /// <returns></returns> private static string StringFormat( string str, Type type) { if (type == typeof ( string )) { str = String2Json(str); str = "\"" + str + "\"" ; } else if (type == typeof (DateTime)) { str = "\"" + str + "\"" ; } else if (type == typeof ( bool )) { str = str.ToLower(); } else if (type != typeof ( string ) && string .IsNullOrEmpty(str)) { str = "\"" + str + "\"" ; } return str; } #endregion #region list转换成JSON /// <summary> /// list转换为Json /// </summary> /// <typeparam name="T"></typeparam> /// <param name="list"></param> /// <returns></returns> public static string ListToJson<T>(IList<T> list) { object obj = list[0]; return ListToJson<T>(list, obj.GetType().Name); } /// <summary> /// list转换为json /// </summary> /// <typeparam name="T1"></typeparam> /// <param name="list"></param> /// <param name="p"></param> /// <returns></returns> private static string ListToJson<T>(IList<T> list, string JsonName) { StringBuilder Json = new StringBuilder(); if ( string .IsNullOrEmpty(JsonName)) JsonName = list[0].GetType().Name; Json.Append( "{\"" + JsonName + "\":[" ); if (list.Count > 0) { for ( int i = 0; i < list.Count; i++) { T obj = Activator.CreateInstance<T>(); PropertyInfo[] pi = obj.GetType().GetProperties(); Json.Append( "{" ); for ( int j = 0; j < pi.Length; j++) { Type type = pi[j].GetValue(list[i], null ).GetType(); Json.Append( "\"" + pi[j].Name.ToString() + "\":" + StringFormat(pi[j].GetValue(list[i], null ).ToString(), type)); if (j < pi.Length - 1) { Json.Append( "," ); } } Json.Append( "}" ); if (i < list.Count - 1) { Json.Append( "," ); } } } Json.Append( "]}" ); return Json.ToString(); } #endregion #region 对象转换为Json /// <summary> /// 对象转换为json /// </summary> /// <param name="jsonObject">json对象</param> /// <returns>json字符串</returns> public static string ToJson( object jsonObject) { string jsonString = "{" ; PropertyInfo[] propertyInfo = jsonObject.GetType().GetProperties(); for ( int i = 0; i < propertyInfo.Length; i++) { object objectValue = propertyInfo[i].GetGetMethod().Invoke(jsonObject, null ); string value = string .Empty; if (objectValue is DateTime || objectValue is Guid || objectValue is TimeSpan) { value = "'" + objectValue.ToString() + "'" ; } else if (objectValue is string ) { value = "'" + ToJson(objectValue.ToString()) + "'" ; } else if (objectValue is IEnumerable) { value = ToJson((IEnumerable)objectValue); } else { value = ToJson(objectValue.ToString()); } jsonString += "\"" + ToJson(propertyInfo[i].Name) + "\":" + value + "," ; } jsonString.Remove(jsonString.Length - 1, jsonString.Length); return jsonString + "}" ; } #endregion #region 对象集合转换为json /// <summary> /// 对象集合转换为json /// </summary> /// <param name="array">对象集合</param> /// <returns>json字符串</returns> public static string ToJson(IEnumerable array) { string jsonString = "{" ; foreach ( object item in array) { jsonString += ToJson(item) + "," ; } jsonString.Remove(jsonString.Length - 1, jsonString.Length); return jsonString + "]" ; } #endregion #region 普通集合转换Json /// <summary> /// 普通集合转换Json /// </summary> /// <param name="array">集合对象</param> /// <returns>Json字符串</returns> public static string ToArrayString(IEnumerable array) { string jsonString = "[" ; foreach ( object item in array) { jsonString = ToJson(item.ToString()) + "," ; } jsonString.Remove(jsonString.Length - 1, jsonString.Length); return jsonString + "]" ; } #endregion #region DataSet转换为Json /// <summary> /// DataSet转换为Json /// </summary> /// <param name="dataSet">DataSet对象</param> /// <returns>Json字符串</returns> public static string ToJson(DataSet dataSet) { string jsonString = "{" ; foreach (DataTable table in dataSet.Tables) { jsonString += "\"" + table.TableName + "\":" + ToJson(table) + "," ; } jsonString = jsonString.TrimEnd( ',' ); return jsonString + "}" ; } #endregion #region Datatable转换为Json /// <summary> /// Datatable转换为Json /// </summary> /// <param name="table">Datatable对象</param> /// <returns>Json字符串</returns> public static string ToJson(DataTable dt) { StringBuilder jsonString = new StringBuilder(); jsonString.Append( "[" ); DataRowCollection drc = dt.Rows; for ( int i = 0; i < drc.Count; i++) { jsonString.Append( "{" ); for ( int j = 0; j < dt.Columns.Count; j++) { string strKey = dt.Columns[j].ColumnName; string strValue = drc[i][j].ToString(); Type type = dt.Columns[j].DataType; jsonString.Append( "\"" + strKey + "\":" ); strValue = StringFormat(strValue, type); if (j < dt.Columns.Count - 1) { jsonString.Append(strValue + "," ); } else { jsonString.Append(strValue); } } jsonString.Append( "}," ); } jsonString.Remove(jsonString.Length - 1, 1); jsonString.Append( "]" ); return jsonString.ToString(); } /// <summary> /// DataTable转换为Json /// </summary> public static string ToJson(DataTable dt, string jsonName) { StringBuilder Json = new StringBuilder(); if ( string .IsNullOrEmpty(jsonName)) jsonName = dt.TableName; Json.Append( "{\"" + jsonName + "\":[" ); if (dt.Rows.Count > 0) { for ( int i = 0; i < dt.Rows.Count; i++) { Json.Append( "{" ); for ( int j = 0; j < dt.Columns.Count; j++) { Type type = dt.Rows[i][j].GetType(); Json.Append( "\"" + dt.Columns[j].ColumnName.ToString() + "\":" + StringFormat(dt.Rows[i][j].ToString(), type)); if (j < dt.Columns.Count - 1) { Json.Append( "," ); } } Json.Append( "}" ); if (i < dt.Rows.Count - 1) { Json.Append( "," ); } } } Json.Append( "]}" ); return Json.ToString(); } #endregion #region DataReader转换为Json /// <summary> /// DataReader转换为Json /// </summary> /// <param name="dataReader">DataReader对象</param> /// <returns>Json字符串</returns> public static string ToJson(DbDataReader dataReader) { StringBuilder jsonString = new StringBuilder(); jsonString.Append( "[" ); while (dataReader.Read()) { jsonString.Append( "{" ); for ( int i = 0; i < dataReader.FieldCount; i++) { Type type = dataReader.GetFieldType(i); string strKey = dataReader.GetName(i); string strValue = dataReader[i].ToString(); jsonString.Append( "\"" + strKey + "\":" ); strValue = StringFormat(strValue, type); if (i < dataReader.FieldCount - 1) { jsonString.Append(strValue + "," ); } else { jsonString.Append(strValue); } } jsonString.Append( "}," ); } dataReader.Close(); jsonString.Remove(jsonString.Length - 1, 1); jsonString.Append( "]" ); return jsonString.ToString(); } #endregion } |
栏目列表
最新更新
python爬虫及其可视化
使用python爬取豆瓣电影短评评论内容
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比