General project we often use serialization and de-serialization of data Json, in order to facilitate the rapid use where needed, general recommendations are packaged as a static extension methods can be used directly where it is needed.
At present, the sequence of the C # project are generally used Newtonsoft.Json to do, so here posted universal static extension method for your reference.
PS: To add this extension method, you have to be added in Nuget package Newtonsoft.Json reference.
the using the System; the using the System.Collections.Generic; the using the System.Text; the using Newtonsoft.Json; the using Newtonsoft.Json.Converters; the using Newtonsoft.Json.Serialization; namespace DemoFrame_Basic.Extensions { /// <Summary> /// static helper class /// </ Summary> public static class ObjStrExtension { /// <Summary> /// returns string Json /// </ Summary> /// <param name = "obj"> subject embodiment requires serialization : T.ToJson () </ param> /// <param name = "isNullValueHandling"> whether to ignore Null fields, the final string contains Null fields </ param> /// <param name = "indented"> whether the display format is a string having Json </ param> / // <param name = "isLowCase"> whether to ignore case </ param> /// <param name = "DateTimeFormat"> time format conversion </ param> /// <Returns> Json string </ Returns> public static String toJSON ( the this Object obj, BOOL isNullValueHandling = to false , BOOL indented = to false ,bool isLowCase = false, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss") { var options = new JsonSerializerSettings(); if (indented) options.Formatting = Formatting.Indented; if (isLowCase) { options.ContractResolver = new CamelCasePropertyNamesContractResolver(); } if (isNullValueHandling) options.NullValueHandling = NullValueHandling.Ignore; options.ReferenceLoopHandling =ReferenceLoopHandling.Ignore; options.Converters = new new List <JsonConverter> { new new IsoDateTimeConverter the DateTimeFormat = { DateTimeFormat}}; return obj.ToJson (Options); } /// <Summary> /// Json string /// </ Summary > /// <param name = "obj"> required sequence of the target Example: T.ToJson (settings) </ param> /// <param name = "settings"> Json serialization settings </ param> // / <Returns> Json string </ returns> public static string ToJson(this Object obj, JsonSerializerSettings Settings) { return JsonConvert.SerializeObject (obj, Settings); } /// <Summary> /// Returns string formatting associated /// </ Summary> /// <param name = "obj "> </ param> /// <param name =" the format "> format Example: the mM-dd-YYYY HH: mm: SS </ param> /// <Returns> formatting string </ Returns> public static String ToDateTimeString ( the this Object obj, String format) { DateTime.TryParse (obj ?.ToString(), out var dateTime); return dateTime.ToString(format); } /// <summary> /// 得到字符串的Byte /// </summary> /// <param name="str"></param> /// <returns>Byte</returns> public static byte[] GetBytes(this string str) { if (string.IsNullOrEmpty(str)) return default(byte[]); return Encoding.UTF8.GetBytes(str); } public static bool ToBool(this string str, bool defaultValue = false) { bool.TryParse(str, out defaultValue); return defaultValue; } public static short ToShort(this string str, short defaultValue = 0) { short.TryParse(str, out defaultValue); return defaultValue; } public static int ToInt(this string str, int defaultValue = 0) { int.TryParse(str, out defaultValue); return defaultValue; } public static long ToLong(this string str, long defaultValue = 0) { long.TryParse(str, out defaultValue); return defaultValue; } public static double ToDouble(this string str, double defaultValue = 0) { double.TryParse(str, out defaultValue); return defaultValue; } public static TEnum ToEnum<TEnum>(this string str, bool ignoreCase = true, TEnum defaultValue = default(TEnum)) where TEnum : struct { Enum.TryParse(str, ignoreCase, out defaultValue); return defaultValue; } public static T ToNetType<T>(this string str, bool isIgnoreNull = true, bool isIgnoreEx = false) { var setting = new JsonSerializerSettings { NullValueHandling = isIgnoreNull ? NullValueHandling.Ignore : NullValueHandling.Include }; try { if (string.IsNullOrEmpty(str)) { return default(T); } else if ("\"\"" == str) { return default(T); } else { return JsonConvert.DeserializeObject<T>(str, setting); } } catch (Exception) { if (!isIgnoreEx) throw; return default(T); } } public static T ToNetType<T>(this string str, JsonSerializerSettings settings) { try { if (string.IsNullOrEmpty(str)) { return default(T); } else if ("\"\"" == str) { return default(T); } else { return JsonConvert.DeserializeObject<T>(str, settings); } } catch (Exception) { return default(T); } } /// <summary> /// 比较是否相等,忽略大小写 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public static bool EqualsIgnoreCase(this string a, string b) { if (a != null) return a.Equals(b, StringComparison.CurrentCultureIgnoreCase); return b == null; } } }