• 编程题:1. var person = '{name:"Lily",sex:"famale",age:24,country:"US"}';将person转换成JSON对象并便利每个属性值。


    /// <summary>
        /// Json工具类
        /// </summary>
        public class JsonUtility
        {
            private static JsonUtility _instance = new JsonUtility();
    
            /// <summary>
            /// 单例
            /// </summary>
            public static JsonUtility Instance
            {
                get { return _instance; }
                set { _instance = value; }
            }
    
            /// <summary>
            /// Json转对象
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="json"></param>
            /// <returns></returns>
            public T JsonToObject<T>(string json)
            {
                var ser = new DataContractJsonSerializer(typeof(T));
                var ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
                var jsonObject = (T)ser.ReadObject(ms);
                ms.Close();
                return jsonObject;
            }
    
            /// <summary>
            /// Json转对象列表
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="json"></param>
            /// <returns></returns>
            public IList<T> JsonToObjectList<T>(string json)
            {
                json = json.Replace("}]}", "");
                var startIndex = json.IndexOf(":[{") + 3;
                var newJson = json.Substring(startIndex);
                var regex = new Regex("},{");
                var jsons = regex.Split(newJson);
    
                if (newJson.Contains("":[]}"))
                {
                    throw new Exception("快件单号没有找到");
                }
                var list = new List<T>();
    
                foreach (var item in jsons)
                {
                    var temp = "{" + item + "}";
                    list.Add(JsonToObject<T>(temp));
                }
                return list;
            }
    
            /// <summary>
            /// 对象转Json
            /// </summary>
            /// <param name="obj"></param>
            /// <returns></returns>
            public string ObjectToJson(object obj)
            {
                var serializer = new DataContractJsonSerializer(obj.GetType());
                using (var ms = new MemoryStream())
                {
                    serializer.WriteObject(ms, obj);
                    var sb = new StringBuilder();
                    sb.Append(Encoding.UTF8.GetString(ms.ToArray()));
                    return sb.ToString();
                }
            }
    
            /// <summary>
            /// 对象列表转Json
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="objectList"></param>
            /// <returns></returns>
            public string ObjectListToJson<T>(IList<T> objectList)
            {
                return ObjectListToJson(objectList, "");
            }
    
            /// <summary>
            /// 对象列表转Json
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="objectList"></param>
            /// <param name="className"></param>
            /// <returns></returns>
            public string ObjectListToJson<T>(IList<T> objectList, string className)
            {
                var sbResult = new StringBuilder();
                sbResult.Append("{");
                className = string.IsNullOrEmpty(className) ? objectList[0].GetType().Name : className;
                sbResult.Append(""" + className + "":[");
    
                for (var i = 0; i < objectList.Count; i++)
                {
                    var item = objectList[i];
                    if (i > 0)
                    {
                        sbResult.Append(",");
                    }
                    sbResult.Append(ObjectToJson(item));
                }
    
                sbResult.Append("]}");
                return sbResult.ToString();
            }
        }
  • 相关阅读:
    firefox native extension -- har export trigger
    配置jenkins slave 问题,ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job.
    尝试用selenium+appium运行一个简单的demo报错:could not get xcode version. /Library/Developer/Info.plist doest not exist on disk
    ruby 除法运算
    ERB预处理ruby代码
    ruby self.included用法
    ruby include和exclude区别
    symfony安装使用
    解决git中文乱码
    读《微软的软件测试之道》有感(上)
  • 原文地址:https://www.cnblogs.com/dangjunhui/p/5476572.html
Copyright © 2020-2023  润新知