C# DataSet转JSON
生活随笔
收集整理的這篇文章主要介紹了
C# DataSet转JSON
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
經(jīng)常會(huì)遇到系統(tǒng)數(shù)據(jù)交互采用JSON數(shù)據(jù)格式進(jìn)行交互的,避免不必要的重復(fù)工作,記錄下自己的處理方式。
獲取數(shù)據(jù)集之后,通過函數(shù)對數(shù)據(jù)集信息進(jìn)行整理通過.Net Framework3.5提出的JavaScriptSerializer類進(jìn)行DataSet數(shù)據(jù)的序列化,
需要添加System.Web.Extensions引用:
private static string DataToJson(DataSet metaData,string msg){ DataTable resultInfo = new DataTable("success");resultInfo.Columns.Add("RETURN_CODE", Type.GetType("System.String"));resultInfo.Columns.Add("ERROR_MSG", Type.GetType("System.String"));DataRow newRow = resultInfo.NewRow();if (metaData == null || metaData.Tables.Count <= 0 || metaData.Tables[0].Rows.Count <= 0){newRow["RETURN_CODE"] = "-1";if (string.IsNullOrEmpty(msg)){newRow["ERROR_MSG"] = "查詢結(jié)果為空";}else{newRow["ERROR_MSG"] = msg;}resultInfo.Rows.Add(newRow);metaData.Tables.Add(resultInfo);}else{newRow["RETURN_CODE"] = "0";newRow["ERROR_MSG"] = "";resultInfo.Rows.Add(newRow);metaData.Tables.Add(resultInfo);}StringBuilder sb = new StringBuilder();sb.Append("{");JavaScriptSerializer serializer = new JavaScriptSerializer();foreach (DataTable dt in metaData.Tables){sb.Append(string.Format("\"{0}\":",dt.TableName));ArrayList arrayList=new ArrayList();foreach (DataRow dataRow in dt.Rows){Dictionary<string, object> dictionary = new Dictionary<string, object>();foreach (DataColumn dataColumn in dt.Columns){dictionary.Add(dataColumn.ColumnName,dataRow[dataColumn.ColumnName]);}arrayList.Add(dictionary);}sb.Append(serializer.Serialize(arrayList));sb.Append(",");}return sb.Remove(sb.Length - 1, 1).Append("}").ToString();}? 此時(shí)我們獲取了一個(gè)JSON格式的字符串,在接收方同樣可以通過JavaScriptSerializer將字符串轉(zhuǎn)換為自己需要的數(shù)據(jù)格式如ArrayList:
private ArrayList JsonToList(string json){JavaScriptSerializer serializer = new JavaScriptSerializer();Dictionary<string,object> dictionary= serializer.Deserialize<Dictionary<string,object>>(json);return (ArrayList)dictionary["data"];}? ? ? 也可以將JSON轉(zhuǎn)換為對象:
?
internal bool ValidateWebJsonValues(ref UserInfo userInfo, string userName, string passWord){try{string json = "{"data":{"accounts":null,"delFlag":"0","email":null,"emailState":null,"instOrgId":null,"institue":false,"member":false,"mobile":null,"org":false,"realName":"包文強(qiáng)","state":"0","teacher":true,"userId":121438653944262,"userType":"2"},"success":true}";JavaScriptSerializer jsSerializer = new JavaScriptSerializer();Dictionary<string, object> dic = jsSerializer.Deserialize<Dictionary<string, object>>(json);bool dataResult = (bool)dic["success"];if (dataResult){Dictionary<string, object> dataDic = (Dictionary<string, object>)dic["data"];Type type = typeof(UserInfo);PropertyInfo[] propInfo = type.GetProperties();foreach (PropertyInfo prop in propInfo){if (dataDic[prop.Name] != null){prop.SetValue(userInfo, dataDic[prop.Name], null);}else{prop.SetValue(userInfo, "", null);}}}return dataResult;}catch (Exception ex){return false;}}以上代碼是根據(jù)個(gè)人過往處理這方面問題的代碼進(jìn)行簡單調(diào)整,也涉及到一些判斷邏輯。作為參考,可以修改調(diào)整后使用。
轉(zhuǎn)載于:https://www.cnblogs.com/ultimateWorld/p/6062303.html
總結(jié)
以上是生活随笔為你收集整理的C# DataSet转JSON的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用svnsync同步svn
- 下一篇: C#中break,continue,re