1. 程式人生 > 實用技巧 >json轉換幫助類

json轉換幫助類

    /// <summary>
    /// json轉換幫助類
    /// </summary>
    public static class JsonHelper
    {
        /*
         
        例項:

            JObject obj = new JObject();
            obj.Add("code", "0");
            obj.Add("msg", "成功");

            JArray jarr = new JArray();

            JObject now = new JObject();
            now.Add("id", 100);
            now.Add("name", "關羽");
            now.Add("date", "2020-07-21 15:05:21");
            jarr.Add(now);

            now = new JObject();
            now.Add("id", 101);
            now.Add("name", "張飛");
            now.Add("date", "2020-07-21 15:06:21");
            jarr.Add(now);

            obj.Add("data", jarr);

            TestClass test = JsonHelper.ToObject<TestClass>(obj.ToString());
            string json = test.ToJson("yyyy-MM-dd HH:mm:ss");
            JObject testClass = JsonHelper.ToJObject(obj.ToString());




        public class TestClass
        {
            public int code { set; get; }
            public string msg { set; get; }
            public List<dataClass> data { set; get; }

            public class dataClass
            {
                public string id { set; get; }

                public string name { set; get; }

                public DateTime date { set; get; }
            }
        }


        
*/ /// <summary> /// 字串是否為空 /// </summary> /// <param name="value"></param> /// <returns></returns> public static bool IsNotEmpty(this string value) { return string.IsNullOrWhiteSpace(value) == false; }
/// <summary> /// model物件轉為json字串 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static string ToJson(this object obj) { return obj == null ? null : JsonConvert.SerializeObject(obj); }
/// <summary> /// model物件轉為json字串 /// </summary> /// <param name="obj"></param> /// <param name="datetimeformats">時間格式,如:yyyy-MM-dd HH:mm:ss yyyy/MM/dd HH:mm:ss</param> /// <returns></returns> public static string ToJson(this object obj, string datetimeformats) { var timeConverter = new IsoDateTimeConverter { DateTimeFormat = datetimeformats }; return JsonConvert.SerializeObject(obj, timeConverter); } /// <summary> /// json字串轉為model物件 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="Json"></param> /// <returns></returns> public static T ToObject<T>(this string Json) { try { return Json.IsNotEmpty() == false ? default(T) : JsonConvert.DeserializeObject<T>(Json); } catch (Exception ex) { return default(T); } } /// <summary> /// json字串轉為泛型集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="Json"></param> /// <returns></returns> public static List<T> ToList<T>(this string Json) { return Json == null ? null : JsonConvert.DeserializeObject<List<T>>(Json); } /// <summary> /// json字串轉為DataTable表格 /// </summary> /// <param name="Json"></param> /// <returns></returns> public static DataTable ToTable(this string Json) { return Json == null ? null : JsonConvert.DeserializeObject<DataTable>(Json); } /// <summary> /// json字串轉為json物件 /// </summary> /// <param name="Json"></param> /// <returns></returns> public static JObject ToJObject(this string Json) { return Json == null ? JObject.Parse("{}") : JObject.Parse(Json.Replace("&nbsp;", "")); } }