json字符串转换为dataset(datatable)

///jsong字符串:[{ “success”: true,“message”: “请求成功”,“data”: [{“字段1”: “值1” }, {“字段2”: “值2” }, {“字段3”: “值3” }, {“字段4”: “值4” }]}]
///返回结果:返回的dataset中有两个datatable,resTable是返回值内容,dataTable是data内容

public static Dat aSet ToDat aSet (string json)
{
	Dat aSet dat aSet=new Dat aSet () ://突例化
	DataTable resTable = new DataTable(" resTable"): //突 例化并指定名称;DataTable dataTable = new DataTable(" dataTable"): 1/突 例化并指定名称DataSet result = dat aSet :try

	J.avaScriptSerializer javaScriptSerializer = new JavaScriptSerializer() :javaScript Serializer. MaxJsonLength = Int32. MaxValue: //取得最大数値
	ArrayList arrayList = javaScriptSerializer. Deserialize<ArrayList> (json) :if (arr ayList. Count > O){

	foreach (DictionaryKstring, object> dictionary in arrayList)
	{
		if (dict ionarx, Keys. Countsstrins20.= , O)
		{
			result = dataSet :
			return result :
		}
		//--Columns
		if (dataTable. Columns. Count == O)

		foreach (string current in dictionary. Keys)
		{
			if (current != "data")
				resTable.Columns.Add (current, dictionary [current]. GetType 0)) :
			else
			{
				ArrayListlist = dictionary[current] as ArrayList :
				foreach (Dictionary<string, object〉 dic in list)
				{
					foreach (string key in dic. Keys)
					{
						dataTable.Columns.Add (key, dic [key]. GetType()) :
					}
					brealk :
				}
			}
		}
	}
	//--Rows 
	string root = "" :
	DạtaRow resRow = resTable.NewRow() :
	foreach (string current in dictionary. Keys)
	{
		if (current!=" data" )
		{
			resRow [current] = dictionary [current]. ToStringO :
		}
	}
	resTable. Rows. Add (resRow) :	


	foreach (string current in dictionary. Keys)
	{
		ịf (current = "data")
		{
			ArrayList list = dictionary[current] as ArrayList :
			foreach (Dict ionary<string, object> dic in list)
			{
				DạtaRow dataRow = dataTable.NewRow() :
				foreach (string key in dic.Keys)
				{
					dat aRow[key] = dic[key] :
				}
				dataTable.Rows.Add (dataRow) :
			}
		}
	}

	çatch (Exceptionẹ) 
	{}
	result.Tables.Add(resTable) :
	result.Tables.Add(dat aTable) :
	return result:
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值