• ObjectToJson,JsonToObject,JsonToObjectList,ObjectListToJson


    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Runtime.Serialization.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();
            }
        } 
  • 相关阅读:
    博客搬到blog.csgrandeur.com
    CSGrandeur的WebGL学习——WebGL教程
    hihoCoder 1160 攻城略地
    HDU 5212 Code
    Ubuntu 14.04 MySQL同步
    Ubuntu 用vsftpd 配置FTP服务器
    Ubuntu14.04 Server amd64 配置 Apache+MySQL+Django
    LeetCode OJ 题解
    MFC+Android模拟器 实现 自动玩“天天爱消除”
    湖南2013第九届省赛解题报告(长期拖延更新中。。。)
  • 原文地址:https://www.cnblogs.com/tearer/p/2759511.html
Copyright © 2020-2023  润新知