.net的dataset,datatable,object等对象转json方法(转)
从anthom里面抽出.net部分数据专json的方法,包括特殊字符的转换,日期等js数据类型初始化:
public class Converter
{
private static void WriteDataRow(StringBuilder sb, DataRow row)
private static void WriteDataSet(StringBuilder sb, DataSet ds)
private static void WriteDataTable(StringBuilder sb, DataTable table)
private static void WriteEnumerable(StringBuilder sb, IEnumerable e)
private static void WriteHashtable(StringBuilder sb, Hashtable e)
private static void WriteObject(StringBuilder sb, object o)
{
MemberInfo[] members = o.GetType().GetMembers(BindingFlags.Instance | BindingFlags.Public);
sb.Append("{");
bool hasMembers = false;
foreach (MemberInfo member in members)
{
bool hasValue = false;
object val = null;
if ((member.MemberType & MemberTypes.Field) == MemberTypes.Field)
{
FieldInfo field = (FieldInfo)member;
val = field.GetValue(o);
hasValue = true;
}
else if ((member.MemberType & MemberTypes.Property) == MemberTypes.Property)
{
PropertyInfo property = (PropertyInfo)member;
if (property.CanRead && property.GetIndexParameters().Length == 0)
{
val = property.GetValue(o, null);
hasValue = true;
}
}
if (hasValue)
{
sb.Append("\"");
sb.Append(member.Name);
sb.Append("\":");
WriteValue(sb, val);
sb.Append(",");
hasMembers = true;
}
}
if (hasMembers)
{
--sb.Length;
}
sb.Append("}");
}
private static void WriteString(StringBuilder sb, string s)
public static void WriteValue(StringBuilder sb, object val)
public static string Convert2Json(object o)
{
StringBuilder sb = new StringBuilder();
WriteValue(sb, o);
return sb.ToString();
}
}
{
private static void WriteDataRow(StringBuilder sb, DataRow row)
private static void WriteDataSet(StringBuilder sb, DataSet ds)
private static void WriteDataTable(StringBuilder sb, DataTable table)
private static void WriteEnumerable(StringBuilder sb, IEnumerable e)
private static void WriteHashtable(StringBuilder sb, Hashtable e)
private static void WriteObject(StringBuilder sb, object o)
{
MemberInfo[] members = o.GetType().GetMembers(BindingFlags.Instance | BindingFlags.Public);
sb.Append("{");
bool hasMembers = false;
foreach (MemberInfo member in members)
{
bool hasValue = false;
object val = null;
if ((member.MemberType & MemberTypes.Field) == MemberTypes.Field)
{
FieldInfo field = (FieldInfo)member;
val = field.GetValue(o);
hasValue = true;
}
else if ((member.MemberType & MemberTypes.Property) == MemberTypes.Property)
{
PropertyInfo property = (PropertyInfo)member;
if (property.CanRead && property.GetIndexParameters().Length == 0)
{
val = property.GetValue(o, null);
hasValue = true;
}
}
if (hasValue)
{
sb.Append("\"");
sb.Append(member.Name);
sb.Append("\":");
WriteValue(sb, val);
sb.Append(",");
hasMembers = true;
}
}
if (hasMembers)
{
--sb.Length;
}
sb.Append("}");
}
private static void WriteString(StringBuilder sb, string s)
public static void WriteValue(StringBuilder sb, object val)
public static string Convert2Json(object o)
{
StringBuilder sb = new StringBuilder();
WriteValue(sb, o);
return sb.ToString();
}
}