C# DataSet转JSON
生活随笔
收集整理的這篇文章主要介紹了
C# DataSet转JSON
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
經常會遇到系統數據交互采用JSON數據格式進行交互的,避免不必要的重復工作,記錄下自己的處理方式。
獲取數據集之后,通過函數對數據集信息進行整理通過.Net Framework3.5提出的JavaScriptSerializer類進行DataSet數據的序列化,
需要添加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"] = "查詢結果為空";}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();}? 此時我們獲取了一個JSON格式的字符串,在接收方同樣可以通過JavaScriptSerializer將字符串轉換為自己需要的數據格式如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轉換為對象:
?
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":"包文強","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;}}以上代碼是根據個人過往處理這方面問題的代碼進行簡單調整,也涉及到一些判斷邏輯。作為參考,可以修改調整后使用。
轉載于:https://www.cnblogs.com/ultimateWorld/p/6062303.html
總結
以上是生活随笔為你收集整理的C# DataSet转JSON的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用svnsync同步svn
- 下一篇: JAVA---反射