关于List对象集合转化成Json字符串,动态赋值问题

时间:2021-08-18 22:14:54
论坛上很多都是List中只是简单的一个赋值问题,现在我遇见个问题是List中的对象中还有对象或者对象集合的属性,具体转换代码如下

        /// <summary>
        /// List对象ToJson
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="list">泛型对象集</param>
        /// <returns>返回字符串</returns>
        public static string ListToJosn<T>(List<T> list)
        {
            var json = new StringBuilder();

            if (list == null)
            {
                return json.ToString();
            }

            if (list.Count > 0)
            {
                // 反射取得类型的属性等信息
                foreach (var processObj in list)
                {
                    if (processObj.GetType().GetProperties().Length > 2)
                    {
                        // 进行条目的组装
                        json.Append("{");

                        foreach (var property in processObj.GetType().GetProperties())
                        {
                            if (property.GetValue(processObj, null).GetType().IsGenericType)
                            {
                                json.Append("\"" + property.Name + "\":{");

                                var typeOne = property.GetValue(processObj, null).GetType().GetGenericArguments()[0];

                                // 此处是集合的循环
                                 json.Append(ListToJson<>(property.GetValue(processObj, null) as List<>));

                                json.Append("},");
                            }
                            else
                            {
                                json.Append("\"" + property.Name + "\":\"" + property.GetValue(processObj, null) + "\"");

                                json.Append(",");
                            }
                        }

                        json.Remove(json.ToString().LastIndexOf(','), 1);

                        json.Append("}");
                    }
                    else
                    {
                        json.Append("\"" + processObj + "\"");
                    }

                    json.Append(",");
                }

                json.Remove(json.ToString().LastIndexOf(','), 1);
            }

            return json.ToString();
        }

中间标红的部分,不知道应该如何传泛型的类型,求高手帮助

7 个解决方案

#1


Object 类型要可以序列化,用MS提供的方法序列化好好了,读也一样用MS的方法,试试吧

#2


System.Web.Script.Serialization;

自己造*真的那么好玩么?

#3


public class Serialization
    {
        /// <summary>
        /// Json序列化
        /// </summary>
        /// <returns></returns>
        public static string Serialize<T>(T obj)
        {
            try
            {
                MemoryStream ms = new MemoryStream();
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
                serializer.WriteObject(ms, obj);
                byte[] json = ms.ToArray();
                ms.Close();
                return Encoding.UTF8.GetString(json, 0, json.Length);
            }
            catch(Exception ex)
            {
                Utils.DebugToFile.WriteErr(ex.ToString());
                return "";
            }
        }
        /// <summary>
        /// Json反序列化
        /// </summary>
        /// <param name="jsonString"></param>
        public static T Deserialize<T>(string jsonString)
        {
            T t = default(T);
            try
            {
                if (!string.IsNullOrEmpty(jsonString))
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
                    MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
                    Object obj = serializer.ReadObject(stream);
                    t = (T)obj;
                }
                return t;
            }
            catch(Exception ex)
            {
                Utils.DebugToFile.WriteErr(ex.ToString());
                return t;
            }
        }
    }

#4




不说啥了,请自己google

"System.Web.Script.Serialization"
"json.net"
"newtonsoft.json"

#5


引用 2 楼 yuwenge 的回复:
System.Web.Script.Serialization;

自己造*真的那么好玩么?


这个不是自己造*的问题,这个是由于以前有许多的model没有序列化的头,只有通过这个来处理

#6


引用 4 楼 wanghui0380 的回复:


不说啥了,请自己google

"System.Web.Script.Serialization"
"json.net"
"newtonsoft.json"


公司里面对于引用外部的DLL有限制的,如果能用我早用"json.net"、"newtonsoft.json"了

System.web.Script.Serialization 这个我在3.5框架中没找到(程序使用的是winform)

#7


算了 我还是自己想怎么处理

多谢前面的各位了

开始散分

#1


Object 类型要可以序列化,用MS提供的方法序列化好好了,读也一样用MS的方法,试试吧

#2


System.Web.Script.Serialization;

自己造*真的那么好玩么?

#3


public class Serialization
    {
        /// <summary>
        /// Json序列化
        /// </summary>
        /// <returns></returns>
        public static string Serialize<T>(T obj)
        {
            try
            {
                MemoryStream ms = new MemoryStream();
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
                serializer.WriteObject(ms, obj);
                byte[] json = ms.ToArray();
                ms.Close();
                return Encoding.UTF8.GetString(json, 0, json.Length);
            }
            catch(Exception ex)
            {
                Utils.DebugToFile.WriteErr(ex.ToString());
                return "";
            }
        }
        /// <summary>
        /// Json反序列化
        /// </summary>
        /// <param name="jsonString"></param>
        public static T Deserialize<T>(string jsonString)
        {
            T t = default(T);
            try
            {
                if (!string.IsNullOrEmpty(jsonString))
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
                    MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
                    Object obj = serializer.ReadObject(stream);
                    t = (T)obj;
                }
                return t;
            }
            catch(Exception ex)
            {
                Utils.DebugToFile.WriteErr(ex.ToString());
                return t;
            }
        }
    }

#4




不说啥了,请自己google

"System.Web.Script.Serialization"
"json.net"
"newtonsoft.json"

#5


引用 2 楼 yuwenge 的回复:
System.Web.Script.Serialization;

自己造*真的那么好玩么?


这个不是自己造*的问题,这个是由于以前有许多的model没有序列化的头,只有通过这个来处理

#6


引用 4 楼 wanghui0380 的回复:


不说啥了,请自己google

"System.Web.Script.Serialization"
"json.net"
"newtonsoft.json"


公司里面对于引用外部的DLL有限制的,如果能用我早用"json.net"、"newtonsoft.json"了

System.web.Script.Serialization 这个我在3.5框架中没找到(程序使用的是winform)

#7


算了 我还是自己想怎么处理

多谢前面的各位了

开始散分