您的位置:首页 > 其它

交换机端口安全 端口的粘性学习

2010-05-03 10:04 281 查看
using

System;


using

System.Data;

using

System.Text;

using

System.Collections.Generic;

using

System.Reflection;

/// <summary>

/// 将DataTable或Ilist<>转换成JSON格式

/// </summary>

public

class

ToJson

{


public

ToJson()


{


}

//DataTable转成Json


public

static

string

DataTableToJson(

string

jsonName, DataTable dt)


{


StringBuilder Json =

new

StringBuilder();


Json.Append(

"{\""

+ jsonName +

"\":["

);


if

(dt.Rows.Count > 0)


{


for

(

int

i = 0; i < dt.Rows.Count; i++)


{


Json.Append(

"{"

);


for

(

int

j = 0; j < dt.Columns.Count; j++)


{


Json.Append(

"\""

+ dt.Columns[j].ColumnName.ToString() +

"\":\""

+ dt.Rows[i][j].ToString() +

"\""

);


if

(j < dt.Columns.Count - 1)


{


Json.Append(

","

);


}


}


Json.Append(

"}"

);


if

(i < dt.Rows.Count - 1)


{


Json.Append(

","

);


}


}


}


Json.Append(

"]}"

);


return

Json.ToString();


}

//List转成json


public

static

string

ObjectToJson<T>(

string

jsonName, IList<T> IL)


{


StringBuilder Json =

new

StringBuilder();


Json.Append(

"{\""

+ jsonName +

"\":["

);


if

(IL.Count > 0)


{


for

(

int

i = 0; i < IL.Count; i++)


{


T obj = Activator.CreateInstance<T>();


Type type = obj.GetType();


PropertyInfo[] pis = type.GetProperties();


Json.Append(

"{"

);


for

(

int

j = 0; j < pis.Length; j++)


{


Json.Append(

"\""

+ pis[j].Name.ToString() +

"\":\""

+ pis[j].GetValue(IL[i],

null

) +

"\""

);


if

(j < pis.Length - 1)


{


Json.Append(

","

);


}


}


Json.Append(

"}"

);


if

(i < IL.Count - 1)


{


Json.Append(

","

);


}


}


}


Json.Append(

"]}"

);


return

Json.ToString();


}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: