可能我們用慣了 Newtonsoft.JSon.dll 等第三方的類庫來實現序列化等類似的操作,但是有時只是簡單的用一下,感覺沒必要那麼費事,所謂殺雞焉用牛刀,自制個小刀即可。
代碼分享給大家,如下:
? 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 318using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Data;
using
System.Reflection;
using
System.Collections;
using
System.Data.Common;
public
class
ConvertJSon
{
#region 私有方法
/// <summary>
/// 過濾特殊字符
/// </summary>
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>
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>
public
static
string
ListToJSon<T>(IList<T> list)
{
object
obj = list[0];
return
ListToJSon<T>(list, obj.GetType().Name);
}
/// <summary>
/// List轉換成JSon
/// </summary>
public
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">對象</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
}