• c# Json List<T> DataTable 数据相互转换


    最近在项目中用到的相关Json的数据转换

    List<T>转Json

            public static string Obj2Json<T>(T data)
            {
                try
                {
                    System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(data.GetType());
                    using (MemoryStream ms = new MemoryStream())
                    {
                        serializer.WriteObject(ms, data);
                        return Encoding.UTF8.GetString(ms.ToArray());
                    }
                }
                catch
                {
                    return null;
                }
            }
    

    Json转List<T>

            public static Object Json2Obj(String json,Type t)
            {
                try
                {
                    System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(t);
                    using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
                    {
                       
                        return  serializer.ReadObject(ms);
                    }
                }
                catch
                {
                    return null;
                }
            }
    
     

    DataTable 转Json

            public static string DataTable2Json(DataTable dt)
            {
                if (dt.Rows.Count == 0)
                {
                    return "";
                }
    
                StringBuilder jsonBuilder = new StringBuilder();
                // jsonBuilder.Append("{"); 
                //jsonBuilder.Append(dt.TableName.ToString());  
                jsonBuilder.Append("[");//转换成多个model的形式
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    jsonBuilder.Append("{");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        jsonBuilder.Append("\"");
                        jsonBuilder.Append(dt.Columns[j].ColumnName);
                        jsonBuilder.Append("\":\"");
                        jsonBuilder.Append(dt.Rows[i][j].ToString());
                        jsonBuilder.Append("\",");
                    }
                    jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                    jsonBuilder.Append("},");
                }
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                jsonBuilder.Append("]");
                //  jsonBuilder.Append("}");
                return jsonBuilder.ToString();
            }
    

    单个对象转JSON

            public static T Json2Obj<T>(string json) 
            {
                T obj = Activator.CreateInstance<T>();
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)))
                {
                    System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
                    return (T)serializer.ReadObject(ms);
                }
            }
    
    
  • 相关阅读:
    Hibernate----面试题
    Java框架部分---面试题
    面试题---多线程
    swap分区
    Linux之格式化与挂载
    Linux下的GPT分区
    Linux下的MBR分区
    vim基础初步
    管道符,通配符以及其他特殊符号
    shell基础之脚本执行,命令别名以及快捷键等
  • 原文地址:https://www.cnblogs.com/mrliuc/p/1934787.html
Copyright © 2020-2023  润新知