Newtonsoft.Json
System.Web
System.Web.Extensions
using System.Web.Script.Serialization;
public class SerializerUtil { #region json public static string SerializeJson(object val) { return JsonConvert.SerializeObject(val, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local, DateFormatString = "yyyy-MM-dd", ContractResolver = new ExcludePropertiesContractResolver() { NotIncludedProperties = new List<string>() { "CreateBy", "CreateOn", "UpdateBy", "UpdateOn" } } }); } public static List<T> JsonStringToList<T>(string str) { JavaScriptSerializer Serializer = new JavaScriptSerializer(); List<T> objs = Serializer.Deserialize<List<T>>(str); return objs; } /// <summary> /// DeSerialize String to Data /// </summary> /// <typeparam name="T">Generic Type</typeparam> /// <param name="str">Encrypt String</param> /// <returns>Type Object</returns> public static T DeSerializeJson<T>(string str) { return (T)DeSerializeJson(typeof(T), str); } public static object DeSerializeJson(Type type, string str) { var scriptSerializer = JsonSerializer.Create(new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local, ContractResolver = new ExcludePropertiesContractResolver() { NotIncludedProperties = new List<string>() { "CreateBy", "CreateOn", "UpdateBy", "UpdateOn" } } }); return JsonConvert.DeserializeObject(str, type); } public static Dictionary<string, object> DeSerializeToDictionary(string jo) { var values = JsonConvert.DeserializeObject<Dictionary<string, object>>(jo); var values2 = new Dictionary<string, object>(); foreach (KeyValuePair<string, object> d in values) { if (d.Value is JObject) { values2.Add(d.Key, DeSerializeToDictionary(d.Value.ToString())); } else { values2.Add(d.Key, d.Value); } } return values2; } #endregion }
ExcludePropertiesContractResolver类,排除一些不必要的字段
public class ExcludePropertiesContractResolver : DefaultContractResolver { public IEnumerable<string> InCludedProperties{get;set;} public IEnumerable<string> NotIncludedProperties { get; set; } public ExcludePropertiesContractResolver() { } protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization) { if (null != NotIncludedProperties && NotIncludedProperties.Count() > 0) { return base.CreateProperties(type, memberSerialization).ToList().FindAll(p => !NotIncludedProperties.Contains(p.PropertyName)); } if (null != InCludedProperties && InCludedProperties.Count() > 0) { return base.CreateProperties(type, memberSerialization).ToList().FindAll(p => InCludedProperties.Contains(p.PropertyName)); } return base.CreateProperties(type, memberSerialization); } }
dll在博客网的文件中
public class InsuranceSolutionEntity { public Guid ID { get; set; } public string Name { get; set; } public string[] Users { get; set; } public List<Algorithm> Algorithms { get; set; } } public class Algorithm { public Guid ID { get; set; } public string ItemName { get; set; } public decimal? Precent { get; set; } public bool Delete { get; set; } }
ID = Guid.NewGuid(), Name = "test", Users = new string[] { "1", "2" }, Algorithms = new List<Algorithm>{ new Algorithm{ ID = Guid.NewGuid(), ItemName ="alg1", Precent = 0.12M }, new Algorithm{ ID = Guid.NewGuid(), ItemName ="alg2", Precent = 0.08M } } }; var json = SerializerUtil.SerializeJson(solution);
相对于contract设置,用Newtonsoft.Json更加人性化

浙公网安备 33010602011771号