之前一直用Newtonsoft.Json来解析数据,但是在List数据解析成json字符串的时候感觉不太好用,不过这里还是把它贴出来。
使用的时候请引用Newtonsoft.Json.dll
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Com.DaTai.BoltTradeClient.Common { /// <summary> /// JSON帮助类 /// </summary> public class JsonHelper { /// <summary> /// 对象转化为JSON格式字符串 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static string Object2Json(object obj) { string result = string.Empty; result = JsonConvert.SerializeObject(obj); return result; } /// <summary> /// JSON格式字符串转化为对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="jsonstring"></param> /// <returns></returns> public static T Json2Object<T>(string jsonstring) { T result; result = JsonConvert.DeserializeObject<T>(jsonstring); return result; } } }
这边提供一个DataContractJsonSerializer 解析json的方法。
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.Serialization.Json; using System.Text; using System.Text.RegularExpressions; using System.Web; namespace WebAPI.Common { public class JsonHelper { /// <summary> /// 加上头文件 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> /// <param name="code"></param> /// <param name="Message"></param> /// <returns></returns> public static string JsonAddHeader<T>(T obj,string code, string Message) { string jsonString = string.Empty; string jsonValue = JsonSerializer(obj); string value = "{\"head\": { \"code\": \"" + code + "\", \"msg\": \"" + Message + "\" }, \"body\":"; jsonString = value + jsonValue + "}"; return jsonString; } /// <summary> /// Json序列化 /// </summary> public static string JsonSerializer<T>(T obj) { string jsonString = string.Empty; try { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); using (MemoryStream ms = new MemoryStream()) { serializer.WriteObject(ms, obj); jsonString = Encoding.UTF8.GetString(ms.ToArray()); } } catch { jsonString = string.Empty; } return jsonString; } /// <summary> /// Json反序列化 /// </summary> public static T JsonDeserialize<T>(string jsonString) { T obj = Activator.CreateInstance<T>(); try { using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString))) { DataContractJsonSerializer ser = new DataContractJsonSerializer(obj.GetType());//typeof(T) T jsonObject = (T)ser.ReadObject(ms); ms.Close(); return jsonObject; } } catch { return default(T); } } } }
单独解析具体对象用:
public List<OrgModel> GetOrgList() { if (UserCache.CACHE_UserInfoModel == null) return null; string url = "api-ums/user/getInfo"; string res = apiHelper.HttpRequest(url, null, RestSharp.Method.GET); ReHeaderModel<object> data = Newtonsoft.Json.JsonConvert.DeserializeObject<ReHeaderModel<object>>(res); if (data == null) return null; if (data.code != 0) return null; else { List<OrgModel> orglist = new List<OrgModel>(); JObject jInfo = JObject.Parse(data.data.ToString()); UserCache.Current_UserId = Convert.ToInt32(jInfo["userid"].ToString()); UserCache.Current_UserName = jInfo["username"].ToString(); var list = jInfo["customer_info_list"]; foreach (var item in list) { if (string.IsNullOrEmpty(item.ToString())) continue; JObject info = JObject.Parse(item.ToString()); OrgModel org = new OrgModel(); org.id = Convert.ToInt32(info["id"]); org.name = info["customer_name"].ToString(); orglist.Add(org); } UserCache.CACHE_OrgModel = orglist; return orglist; } }
文章评论