123456789101112131415161718192021222324252627282930 |
- 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
- {
- /// <summary>
- /// Deserialize json to target class
- /// when jsonStr deserializes failed, put default value in there
- /// </summary>
- public static T DeserializeJSON<T>(string jsonStr)
- {
- T response = default(T);
- response = JsonConvert.DeserializeObject<T>(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;
- }
- }
- }
|