/// <summary> /// Json编码器 /// </summary> /// <typeparam name="T"></typeparam> public class JsonEncode { /// <summary> /// 反序列化 /// </summary> /// <param name="data"></param> /// <returns></returns> public static T? Deserialize<T>(object? data) { try { return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(data)); } catch { return default; } } /// <summary> /// 序列化 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static string Serialize(object obj) { return SerializeObject(obj); } /// <summary> /// 序列化 /// </summary> /// <param name="obj">要序列化的对象</param> /// <returns>json字符串</returns> private static string SerializeObject(object obj) { // Json序列化程序设置 var serializerSettings = new JsonSerializerSettings { // 设置为驼峰命名 ContractResolver = new CamelCasePropertyNamesContractResolver() }; return JsonConvert.SerializeObject(obj, Formatting.None, serializerSettings); } }
翻译
搜索
复制
标签:obj,C#,SerializeObject,Json,static,return,序列化 From: https://www.cnblogs.com/smartnn/p/17633664.html