using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace KdanCommon.Helpers { public static class JsonTool { /// /// Deserialize json to target class /// when jsonStr deserializes failed, put default value in there /// public static T DeserializeJSON(string jsonStr) { T response = default(T); response = JsonConvert.DeserializeObject(jsonStr, new JsonSerializerSettings() { Error = (sender, errorEventArgs) => { System.Diagnostics.Debug.WriteLine(String.Format("error happen in {0} : {1}", errorEventArgs.CurrentObject.GetType().Name, errorEventArgs.ErrorContext.Error.Message)); errorEventArgs.ErrorContext.Handled = true; } }); return response; } } }