C 將任意型別資料轉成JSON格式輸出的類

2021-08-25 15:42:54 字數 2452 閱讀 7099

using system;

using system.data;

using system.configuration;

using system.web;

using system.web.security;

using system.web.ui;

using system.web.ui.webcontrols;

using system.web.ui.webcontrols.webparts;

using system.web.ui.htmlcontrols;

using system.collections.generic;

using system.text;

using system.text.regularexpressions;

///

/// creat_json 任意型別資料轉成json格式資料輸出

///

public class creat_json

///

/// list轉成json

///

///

///

///

///

///

/// list轉成json

///

///

///

///

public static string listtojson(ilistlist)

///

/// 物件轉換為json字串

///

/// 物件

/// json字串

public static string tojson(object jsonobject)

else if (objectvalue is string)

else if (objectvalue is ienumerable)

else

jsonstring += "\"" + tojson(propertyinfo[i].name) + "\":" + value + ",";

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

return jsonstring + "}";

}///

/// 物件集合轉換json

///

/// 集合物件

/// json字串

public static string tojson(ienumerable array)

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

return jsonstring + "]";

}///

/// 普通集合轉換json

///

/// 集合物件

/// json字串

public static string toarraystring(ienumerable array)

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

return jsonstring + "]";

}///

/// datatable轉換為json

///

/// datatable物件

/// json字串

///

/// datatable轉成json

///

///

///

///

///

/// datareader轉換為json

///

/// datareader物件

/// json字串

///

/// dataset轉換為json

///

/// dataset物件

/// json字串

public static string tojson(dataset dataset)

jsonstring = jsonstring.trimend(',');

return jsonstring + "}";

}///

/// 過濾特殊字元

///

///

///

///

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

///

///

///

///

private static string stringformat(string str, type type)

else if (type == typeof(datetime))

else if (type == typeof(bool))

return str;} }

C 中任意型別資料轉成JSON格式

list轉成json public static string listtojson ilistlist,string jsonname if i list.count 1 return json.tostring list轉成json public static string listtojson...

實用C 中任意型別資料轉成JSON格式資料輸出

list轉成json list轉成json public static string listtojson ilistlist 物件轉換為json字串 物件 json字串 public static string tojson object jsonobject else if objectvalu...

C 中任意型別資料轉成JSON格式資料輸出

list轉成json public static string listtojson ilistlist,string jsonname if i list.count 1 return json.tostring list轉成json public static string listtojson...