c 通用json幫助類

2021-06-20 09:07:47 字數 4666 閱讀 2627

using system;

using system.data;

using system.text;

using system.collections.generic;

using system.reflection;

using system.data.common;

using system.collections;

using system.io;

using system.text.regularexpressions;

using system.runtime.serialization.json;

namespace zzudp.tool

else

if (j < pi.length - 1)

}if (i < list.count - 1)}}

return json.tostring();

}/// /// 序列化集合物件

///

public static string jsonserializerbyarraydata(t tarray)

/// /// 序列化單個物件

///

public static string jsonserializerbysingledata(t t)

/// /// 反序列化單個物件

///

public static t jsondeserializebysingledata(string jsonstring)

-\d-\d\s\d:\d:\d";

matchevaluator matchevaluator = new matchevaluator(convertdatestringtojsondate);

regex reg = new regex(p);

jsonstring = reg.replace(jsonstring, matchevaluator);

datacontractjsonserializer ser = new datacontractjsonserializer(typeof(t));

memorystream ms = new memorystream(encoding.utf8.getbytes(jsonstring));

t obj = (t)ser.readobject(ms);

return obj;

}/// /// 反序列化集合物件

///

public static t jsondeserializebyarraydata(string jsonstring)

-\d-\d\s\d:\d:\d";

matchevaluator matchevaluator = new matchevaluator(convertdatestringtojsondate);

regex reg = new regex(p);

jsonstring = reg.replace(jsonstring, matchevaluator);

datacontractjsonserializer ser = new datacontractjsonserializer(typeof(t));

memorystream ms = new memorystream(encoding.utf8.getbytes(jsonstring));

t arrayobj = (t)ser.readobject(ms);

return arrayobj;

}/// /// 將json序列化的時間由/date(1294499956278+0800)轉為字串

///

private static string convertjsondatetodatestring(match m)

/// /// 將時間字串轉為json時間

///

private static string convertdatestringtojsondate(match m)

+0800)\\/", ts.totalmilliseconds);

return result;

}/// /// list轉成json

///

///

///

///

public static string listtojson(ilistlist)

/// /// 物件轉換為json字串

///

/// 物件

/// json字串

public static string tojson(object jsonobject)

stringbuilder value = new stringbuilder();

if (objectvalue is datetime || objectvalue is guid || objectvalue is timespan)

else if (objectvalue is string)

else if (objectvalue is ienumerable)

else

}return jsonstring.tostring().trimend(',') + "}";

}catch (exception ex)

}/// /// 物件集合轉換json

///

/// 集合物件

/// json字串

public static string tojson(ienumerable array)

if (jsonstring.length > 1)

else

return jsonstring + "]";

}/// /// 普通集合轉換json

///

/// 集合物件

/// json字串

public static string toarraystring(ienumerable array)

jsonstring.remove(jsonstring.length - 1, jsonstring.length);

return jsonstring + "]";

}/// /// datatable轉換為json

///

/// datatable物件

/// json字串

public static string tojson(datatable dt)

else}}

jsonstring.remove(jsonstring.length - 1, 1);

if (jsonstring.length == 1)

return jsonstring.tostring();

}/// /// datatable轉成json

///

///

///

///

public static string tojson(datatable dt, string jsonname)

}if (i < dt.rows.count - 1)}}

return json.tostring();

}/// /// datareader轉換為json

///

/// datareader物件

/// json字串

public static string tojson(idatareader datareader)

else}}

if (!datareader.isclosed)

jsonstring.remove(jsonstring.length - 1, 1);

if (jsonstring.length == 1)

return jsonstring.tostring();

}catch (exception ex)

}/// /// dataset轉換為json

///

/// dataset物件

/// json字串

public static string tojson(dataset dataset)

jsonstring = jsonstring.trimend(',');

return jsonstring + "}";

}/// /// 過濾特殊字元

///

///

///

public static string string2json(string s)

}return sb.tostring();

}/// /// 格式化字元型、日期型、布林型

///

///

///

///

private static string stringformat(string str, type type)

else if (type == typeof(string))

else if (type == typeof(datetime))

else if (type == typeof(bool))

else if (type == typeof(byte))

else if (type == typeof(guid))

return str;}}

}

轉換Json格式幫助類

using system using system.collections.generic using system.text using system.reflection using system.data using system.collections namespace aimscommo...

C 物件資料轉換Json幫助類 JsonHelp

c 物件資料轉換json幫助類 jsonhelp using system using system.data using system.configuration using system.web using system.web.security using system.web.ui usin...

C 資料快取介紹及Caching通用幫助類整理

c 快取主要是為了提高資料的讀取速度。因為伺服器和應用客戶端之間存在著流量的瓶頸,所以讀取大容量資料時,使用快取來直接為客戶端服務,可以減少客戶端與伺服器端的資料互動,從而大大提高程式的效能。以下為工作中經常用到的快取操作公用類庫整理 using system using system.collec...