代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
对象转换为Json字符串
作者:
英
/ 发布于
2015/3/24
/
797
/// <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 + ","; } return Json.DeleteLast(jsonString) + "}"; } /// <summary> /// 对象集合转换Json /// </summary> /// <param name="array">集合对象</param> /// <returns>Json字符串</returns> public static string ToJson(IEnumerable array) { string jsonString = "["; foreach (object item in array) { jsonString += Json.ToJson(item) + ","; } return Json.DeleteLast(jsonString) + "]"; } /// <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()) + ","; } return Json.DeleteLast(jsonString) + "]"; } /// <summary> /// 删除结尾字符 /// </summary> /// <param name="str">需要删除的字符</param> /// <returns>完成后的字符串</returns> private static string DeleteLast(string str) { if (str.Length > 1) { return str.Substring(0, str.Length - 1); } return str; } /// <summary> /// Datatable转换为Json /// </summary> /// <param name="table">Datatable对象</param> /// <returns>Json字符串</returns> public static string ToJson(DataTable table) { string jsonString = "["; DataRowCollection drc = table.Rows; for (int i = 0; i < drc.Count; i++) { jsonString += "{"; foreach (DataColumn column in table.Columns) { jsonString += "\"" + ToJson(column.ColumnName) + "\":"; if (column.DataType == typeof(DateTime) || column.DataType == typeof(string)) { jsonString += "\"" + ToJson(drc[i][column.ColumnName].ToString()) + "\","; } else { jsonString += ToJson(drc[i][column.ColumnName].ToString()) + ","; } } jsonString = DeleteLast(jsonString) + "},"; } return DeleteLast(jsonString) + "]"; } /// <summary> /// DataReader转换为Json /// </summary> /// <param name="dataReader">DataReader对象</param> /// <returns>Json字符串</returns> public static string ToJson(DbDataReader dataReader) { string jsonString = "["; while (dataReader.Read()) { jsonString += "{"; for (int i = 0; i < dataReader.FieldCount; i++) { jsonString += "\"" + ToJson(dataReader.GetName(i)) + "\":"; if (dataReader.GetFieldType(i) == typeof(DateTime) || dataReader.GetFieldType(i) == typeof(string)) { jsonString += "\"" + ToJson(dataReader[i].ToString()) + "\","; } else { jsonString += ToJson(dataReader[i].ToString()) + ","; } } jsonString = DeleteLast(jsonString) + "}"; } dataReader.Close(); return DeleteLast(jsonString) + "]"; } /// <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 += "\"" + ToJson(table.TableName) + "\":" + ToJson(table) + ","; } return jsonString = DeleteLast(jsonString) + "}"; } /// <summary> /// String转换为Json /// </summary> /// <param name="value">String对象</param> /// <returns>Json字符串</returns> public static string ToJson(string value) { if (string.IsNullOrEmpty(value)) { return string.Empty; } string temstr; temstr = value; temstr = temstr.Replace("{", "{").Replace("}", "}").Replace(":", ":").Replace(",", ",").Replace("[", "【").Replace("]", "】").Replace(";", ";").Replace("\n", "<br/>").Replace("\r", ""); temstr = temstr.Replace("\t", " "); temstr = temstr.Replace("'", "\'"); temstr = temstr.Replace(@"\", @"\\"); temstr = temstr.Replace("\"", "\"\""); return temstr; }
试试其它关键字
Json
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
英
贡献的其它代码
(
4
)
.
DataTable导入、导出xml文件
.
OpenFileDialog的运用
.
对象转换为Json字符串
.
邮箱附件发送
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3