zoukankan      html  css  js  c++  java
  • JSONHelper

     public class JSONHelper
        {
            /// <summary>
            /// 将对象序列化为JSON格式
            /// </summary>
            /// <param name="o">对象</param>
            /// <returns>json字符串</returns>
            public static string SerializeObject(object o)
            {
                string json = JsonConvert.SerializeObject(o, Formatting.None, _jsonSerializerSettings);
                return json;
            }
    
            /// <summary>
            /// 解析JSON字符串生成对象实体
            /// </summary>
            /// <typeparam name="T">对象类型</typeparam>
            /// <param name="json">json字符串</param>
            /// <returns>对象实体</returns>
            public static T DeserializeJsonToObject<T>(string json) where T : class
            {
                JsonSerializer serializer = new JsonSerializer();
    
                /*******json设置*********/
                //忽略默认值
                serializer.DefaultValueHandling = DefaultValueHandling.Ignore;
    
                StringReader sr = new StringReader(json);
               
                object o = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
                T t = o as T;
                return t;
            }
    
            /// <summary>
            /// 解析JSON数组生成对象实体集合
            /// </summary>
            /// <typeparam name="T">对象类型</typeparam>
            /// <param name="jsonString">json字符串</param>
            /// <returns>对象列表</returns>
            public static List<T> DeserializeJsonToList<T>(String jsonString) where T : class
            {
                JsonSerializer serializer = new JsonSerializer();
                StringReader sr = new StringReader(jsonString);
    
                /*******json设置*********/
                //忽略默认值
                serializer.DefaultValueHandling = DefaultValueHandling.Ignore;
               
                //日期格式
    
                object o = serializer.Deserialize(new JsonTextReader(sr), typeof(List<T>));
                List<T> list = o as List<T>;
                return list;
            }
    
            [JsonIgnore]
            private static readonly JsonSerializerSettings _jsonSerializerSettings
                = new JsonSerializerSettings
                {
                    Converters
                        = new JsonConverter[]
                                    {
                                        new IsoDateTimeConverter
                                            {
                                                DateTimeFormat = "yyyy-MM-dd HH:mm:ss"
                                            }
                                    }
                };
    
            private static JsonSerializerSettings setJsonSerializerSettings()
            {
                JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
                jsonSerializerSettings.DefaultValueHandling = DefaultValueHandling.Ignore;
                //jsonSerializerSettings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat; 
    
                return jsonSerializerSettings;
            }
        }
  • 相关阅读:
    leetcode 29-> Divide Two Integers without using multiplication, division and mod operator
    ros topic 发布一次可能会接收不到数据
    python中的print()、str()和repr()的区别
    python 部分函数
    uiautomatorviewer错误 unable toconnect to adb
    pyqt 不规则形状窗口显示
    appium 计算器demo
    Spring 3.0 注解注入详解
    Spring Autowire自动装配
    restful 学习地址
  • 原文地址:https://www.cnblogs.com/dj258/p/10635296.html
Copyright © 2011-2022 走看看