C# JSON序列化 Newtonsoft.Json 的简单使用 及DLL文件下载

using Newtonsoft.Json; 

//DataTable转json 
public string DtToJson(DataTable table) 
{ 
    string jsonString = string.Empty; 
    jsonString = JsonConvert.SerializeObject(table); 
    return jsonString; 
}
//List转json
public static string ListToJson(List list)
{
    return JsonConvert.SerializeObject(list);
}
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Json;
using System.Text;

namespace Common.Conversion
{
    /// 
    /// JSON序列化
    /// 
    public static class JsonSerialize
    {
        #region JSON 序列化
        /// 
        /// JSON 序列化的普通版本
        /// 
        /// 
        /// 
        /// 
        public static string ObjectToJSON(this T _Object) where T : class
        {
            return JsonConvert.SerializeObject(_Object);
        }

        /// 
        /// JSON 序列化的标准版本
        /// 
        /// 
        /// 
        public static string ObjectToJSON(this Object _Object)
        {
            return JsonConvert.SerializeObject(_Object);
        }

        /// 
        /// JSON 序列化的官方版本
        /// 
        /// 
        /// 
        /// 
        public static string ObjectToJSONOfficial(this T _Object) where T : class
        {
            using (var TempMemoryStream = new MemoryStream())
            {
                var MyDataContractJsonSerializer = new DataContractJsonSerializer(typeof(T));
                MyDataContractJsonSerializer.WriteObject(TempMemoryStream, _Object);
                var MySerializationString = Encoding.UTF8.GetString(TempMemoryStream.ToArray());
                return MySerializationString;
            }
        }

        /// 
        /// DataTable转JSON
        /// 
        /// DataTable
        /// 
        public static string DtToJson(DataTable table)
        {
            string jsonString = string.Empty;
            jsonString = JsonConvert.SerializeObject(table);
            return jsonString;
        }

        /// 
        /// 普通List转JSON
        /// 
        /// 
        /// 
        public static string ListToJson(IList list)
        {
            DataTable table = ListToDataTable(list);
            return DtToJson(table);
        }

        /// 
        /// list转dataTable
        /// 
        /// List集合
        /// 
        private static DataTable ListToDataTable(IList list)
        {
            DataTable result = new DataTable();
            if (list.Count > 0)
            {
                PropertyInfo[] propertys = list[0].GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    Type colType = pi.PropertyType;
                    if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                    {
                        colType = colType.GetGenericArguments()[0];
                    }
                    result.Columns.Add(pi.Name, colType);
                }

                for (int i = 0; i < list.Count; i++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in propertys)
                    {
                        object obj = pi.GetValue(list[i], null);
                        tempList.Add(obj);
                    }
                    object[] array = tempList.ToArray();
                    result.LoadDataRow(array, true);
                }
            }
            return result;
        }

        /// 
        /// 泛型List转JSON
        /// 
        /// 
        /// 
        /// 
        public static string ListToJson(List list)
        {
            return JsonConvert.SerializeObject(list);
        }
        #endregion

        #region JSON 反序列化
        /// 
        /// JSON 的反序列化
        /// 
        /// 
        /// 
        /// 
        public static T JSONToObjectJSON(this string _JSONString) where T : class
        {
            return JsonConvert.DeserializeObject(_JSONString);
        }

        /// 
        /// JSON的反序列化的官方版本
        /// 
        /// 
        /// 
        /// 
        public static T JSONToObjectOfficial(this string _JSONString) where T : class
        {
            using (var TempMemoryStream = new MemoryStream(Encoding.UTF8.GetBytes(_JSONString)))
            {
                var MyDataContractJsonSerializer = new DataContractJsonSerializer(typeof(T));
                return (T)MyDataContractJsonSerializer.ReadObject(TempMemoryStream);
            }
        }
        #endregion
    }
}

下载地址:

https://download.csdn.net/download/djk8888/10695449  (常用版本,3.5,4.0,4.5)

https://download.csdn.net/download/djk8888/11686139 (32位、64位,1.0至4.5.1最全合集)

 

 

你可能感兴趣的:(ASP.NET代码)