.NET JSON扩展

在南京工作时,自己写了个将常用数据转换成json的扩展。今天查找文件时找到了,就把它放上来,免得以后用着时找不到。

毕竟很久之前写的,当时水平所限,漏洞肯定不少。但最近忙,没空改它,就先放着,博客园上的东西应该不会丢^_^

namespace JavenSoft.Extension
{
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Collections;
using System.Data;
using System.Data.Common;

///<summary>
/// 常用数据类型Json扩展
/// 用于将常用的数据类型转换为Json对象
///</summary>
public staticclass JsonExtension
{
///<summary>
/// 把指定的对象转换为Json字符串
///</summary>
///<param name="source">要转换的对象</param>
///<returns>Json字符串</returns>
public staticstring ToJson(thisobject source)
{
StringBuilder jsonBuilder
=new StringBuilder();
jsonBuilder.Append(
"{");

PropertyInfo[] propertyInfo
= source.GetType().GetProperties();

for (int i =0; i < propertyInfo.Length; i++)
{
object objectValue = propertyInfo[i].GetGetMethod().Invoke(source, null);

jsonBuilder.Append(
"\"");
jsonBuilder.Append(propertyInfo[i].Name);
jsonBuilder.Append(
"\":");

string value =string.Empty;
if (objectValue is DateTime || objectValue isstring|| objectValue is Guid || objectValue is TimeSpan)
{
jsonBuilder.Append(
"'");
jsonBuilder.Append(objectValue);
jsonBuilder.Append(
"'");
}
elseif (objectValue is IEnumerable)
{
jsonBuilder.Append((objectValue
as IEnumerable).ToJson());
}
else
{
jsonBuilder.Append(objectValue.ToJson());
}

jsonBuilder.Append(value);
jsonBuilder.Append(
",");
}

jsonBuilder.RemoveLast();
jsonBuilder.Append(
"}");
return jsonBuilder.ToString();
}
///<summary>
/// 对象集合转换Json
///</summary>
///<param name="source">集合对象</param>
///<returns>Json字符串</returns>
public staticstring ToJson<T>(this IEnumerable<T> source) where T : class
{
StringBuilder jsonBuilder
=new StringBuilder();
jsonBuilder.Append(
"[");

foreach (object item in source)
{
jsonBuilder.Append(item.ToJson());
jsonBuilder.Append(
",");
}
jsonBuilder.RemoveLast();
jsonBuilder.Append(
"]");

return jsonBuilder.ToString();
}
///<summary>
/// Datatable转换为Json
///</summary>
///<param name="table">Datatable对象</param>
///<returns>Json字符串</returns>
public staticstring ToJson(this DataTable table)
{
StringBuilder jsonBuilder
=new StringBuilder();
jsonBuilder.Append(
"[");

DataRowCollection drc
= table.Rows;
for (int i =0; i < drc.Count; i++)
{
jsonBuilder.Append(
"{");
foreach (DataColumn column in table.Columns)
{
jsonBuilder.Append(
"\"");
jsonBuilder.Append(column.ColumnName);
jsonBuilder.Append(
"\":");

if (column.DataType ==typeof(DateTime) || column.DataType ==typeof(string))
{
jsonBuilder.Append(
"\"");
jsonBuilder.Append(drc[i][column.ColumnName]);
jsonBuilder.Append(
"\",");
}
else
{
jsonBuilder.Append(drc[i][column.ColumnName]);
jsonBuilder.Append(
",");
}
}
jsonBuilder.RemoveLast();
jsonBuilder.Append(
"},");
}

jsonBuilder.RemoveLast();

jsonBuilder.Append(
"]");
return jsonBuilder.ToString();
}
///<summary>
/// DataReader转换为Json
///</summary>
///<param name="dataReader">DataReader对象</param>
///<returns>Json字符串</returns>
public staticstring ToJson(this DbDataReader dataReader)
{
StringBuilder jsonBuilder
=new StringBuilder();
jsonBuilder.Append(
"[");

while (dataReader.Read())
{
jsonBuilder.Append(
"{");

for (int i =0; i < dataReader.FieldCount; i++)
{
jsonBuilder.Append(
"\"");
jsonBuilder.Append(dataReader.GetName(i));
jsonBuilder.Append(
"\":");

if (dataReader.GetFieldType(i) ==typeof(DateTime) || dataReader.GetFieldType(i) ==typeof(string))
{
jsonBuilder.Append(
"\"");
jsonBuilder.Append(dataReader[i]);
jsonBuilder.Append(
"\",");
}
else
{
jsonBuilder.Append(dataReader[i]);
jsonBuilder.Append(
",");
}
}
jsonBuilder.RemoveLast();
jsonBuilder.Append(
"}");
}
dataReader.Close();
jsonBuilder.RemoveLast();
jsonBuilder.Append(
"]");

return jsonBuilder.ToString();
}
///<summary>
/// DataSet转换为Json
///</summary>
///<param name="dataSet">DataSet对象</param>
///<returns>Json字符串</returns>
public staticstring ToString(this DataSet dataSet)
{
StringBuilder jsonBuilder
=new StringBuilder();
jsonBuilder.Append(
"{");

foreach (DataTable table in dataSet.Tables)
{
jsonBuilder.Append(
"\"");
jsonBuilder.Append(table.TableName);
jsonBuilder.Append(
"\":");
jsonBuilder.Append(table.ToJson());
jsonBuilder.Append(
",");
}

jsonBuilder.RemoveLast();
jsonBuilder.Append(
"}");

return jsonBuilder.ToString();
}
}
}

这里面还用到了另一个扩展,因为都是使用频率特别高的方法,所以做成扩展也不错

namespace JavenSoft.Extension
{
using System.Text;
publicstaticclass StringBuilderExtension
{
///<summary>
/// 删除结尾字符
///</summary>
///<param name="source">需要删除的字符</param>
///<returns>完成后的StringBuilder对象</returns>
public static StringBuilder RemoveLast(this StringBuilder source)
{
return source.RemoveLast(1);
}
///<summary>
/// 删除结尾字符
///</summary>
///<param name="source">需要删除的字符</param>
///<param name="length">要删除的长度</param>
///<returns>完成后的StringBuilder对象</returns>
public static StringBuilder RemoveLast(this StringBuilder source, int length)
{
if (source.Length >= length)
{
return source.Remove(source.Length - length, length);
}
return source;
}
}
}

  

posted @ 2011-08-03 10:56  Baocang  阅读(229)  评论(0编辑  收藏  举报