using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.Serialization.Json; using System.Text; using System.Threading.Tasks; namespace JinYuan.Helper { public class JsonHelper { /// /// 使用Newtonsoft.json.dll对象序列化成Json字符串 /// /// /// /// public static string EntityToJson(T t) { try { return JsonConvert.SerializeObject(t); } catch (Exception) { return string.Empty; } } /// /// 使用Newtonsoft.json.dll 将Json字符串反序列化成对象 /// /// /// /// public static T JsonToEntity(string json) { try { return (T)JsonConvert.DeserializeObject(json, typeof(T)); } catch (Exception) { return default(T); } } /// /// 实体类转换成JSON字符串 /// /// /// /// public static string EntityToJson2(T obj) { //序列化 DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(T)); MemoryStream msObj = new MemoryStream(); //将序列化之后的Json格式数据写入流中 js.WriteObject(msObj, obj); msObj.Position = 0; //从0这个位置开始读取流中的数据 StreamReader sr = new StreamReader(msObj, Encoding.Default); string json = sr.ReadToEnd(); sr.Close(); msObj.Close(); return json; } /// /// JSON字符串转换成实体类 /// /// /// /// public static T JsonToEntity2(string json) where T : class { //反序列化 using (var ms = new MemoryStream(Encoding.Default.GetBytes(json))) { DataContractJsonSerializer deseralizer = new DataContractJsonSerializer(typeof(T)); T model = (T)deseralizer.ReadObject(ms);// //反序列化ReadObject return model; } } } }