在项目中用到了Newtonsoft.Json来实现序列化和反序列化,在这里写下实现代码。
1、创建类用于排除不序列化的属性
public class ExcludePropertiesContractResolver : DefaultContractResolver
{
IEnumerable<string> lstExclude; public ExcludePropertiesContractResolver(IEnumerable<string> excludedProperties)
{
lstExclude = excludedProperties;
} protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
return base.CreateProperties(type, memberSerialization).ToList().FindAll(p => !lstExclude.Contains(p.PropertyName));
}
}
2、序列化方法
以下分别是将对象、DataTable、集合序列化为Json字符串的方法:
/// <summary>
/// T对象转换成json字符串
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static string GetJsonString<T>(T obj, params string[] ignoreFields)
{
IsoDateTimeConverter iso = new IsoDateTimeConverter();
iso.DateTimeFormat = "yyyy-MM-ddTHH:mm:ss";
iso.DateTimeStyles = System.Globalization.DateTimeStyles.AssumeLocal;
string jsonStr = string.Empty;
JsonSerializerSettings js = new JsonSerializerSettings();
if (ignoreFields != null && ignoreFields.Length > 0)
{
List<string> ignoreList = ignoreFields.ToList();
js.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
js.ContractResolver = new ExcludePropertiesContractResolver(ignoreList);
}
js.Converters.Add(iso);
jsonStr = JsonConvert.SerializeObject(obj, Formatting.Indented, js);
return jsonStr;
} /// <summary>
/// DataTable对象转换成json字符串
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static string GetJsonString(DataTable dt, params string[] ignoreFields)
{
IsoDateTimeConverter iso = new IsoDateTimeConverter();
iso.DateTimeFormat = "yyyy-MM-ddTHH:mm:ss";
iso.DateTimeStyles = System.Globalization.DateTimeStyles.AssumeLocal;
string jsonStr = string.Empty;
JsonSerializerSettings js = new JsonSerializerSettings();
if (ignoreFields != null && ignoreFields.Length > 0)
{
List<string> ignoreList = ignoreFields.ToList();
js.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
js.ContractResolver = new ExcludePropertiesContractResolver(ignoreList);
}
js.Converters.Add(new DataTableConverter());
js.Converters.Add(iso);
jsonStr = JsonConvert.SerializeObject(dt, Formatting.Indented,js);
return jsonStr;
} /// <summary>
/// List对象转换成json字符串
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static string GetJsonString<T>(List<T> list, params string[] ignoreFields)
{
IsoDateTimeConverter iso = new IsoDateTimeConverter();
iso.DateTimeFormat = "yyyy-MM-ddTHH:mm:ss";
iso.DateTimeStyles = System.Globalization.DateTimeStyles.AssumeLocal;
string jsonStr = string.Empty;
JsonSerializerSettings js = new JsonSerializerSettings();
if (ignoreFields != null && ignoreFields.Length > 0)
{
List<string> ignoreList = ignoreFields.ToList();
js.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
js.ContractResolver = new ExcludePropertiesContractResolver(ignoreList);
}
js.Converters.Add(iso);
jsonStr = JsonConvert.SerializeObject(list, Formatting.Indented, js);
return jsonStr;
}
3、反序列化
以下是将Json字符串反序列化为对象、集合的方法。
/// <summary>
/// json字符串转换成T对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonStr"></param>
/// <returns></returns>
public static T GetObjectByJsonString<T>(string jsonStr)
{
T obj = JsonConvert.DeserializeObject<T>(jsonStr);
return obj;
} /// <summary>
/// json字符串转换成List对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonStr"></param>
/// <returns></returns>
public static List<T> GetListByJsonString<T>(string jsonStr)
{
List<T> list = JsonConvert.DeserializeObject<List<T>>(jsonStr);
return list;
}