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<T>(List<T> 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
{
    /// <summary>
    /// JSON序列化
    /// </summary>
    public static class JsonSerialize
    {
        #region JSON 序列化
        /// <summary>
        /// JSON 序列化的普通版本
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="_Object"></param>
        /// <returns></returns>
        public static string ObjectToJSON<T>(this T _Object) where T : class
        {
            return JsonConvert.SerializeObject(_Object);
        }

        /// <summary>
        /// JSON 序列化的标准版本
        /// </summary>
        /// <param name="_Object"></param>
        /// <returns></returns>
        public static string ObjectToJSON(this Object _Object)
        {
            return JsonConvert.SerializeObject(_Object);
        }

        /// <summary>
        /// JSON 序列化的官方版本
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="_Object"></param>
        /// <returns></returns>
        public static string ObjectToJSONOfficial<T>(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;
            }
        }

        /// <summary>
        /// DataTable转JSON
        /// </summary>
        /// <param name="table">DataTable</param>
        /// <returns></returns>
        public static string DtToJson(DataTable table)
        {
            string jsonString = string.Empty;
            jsonString = JsonConvert.SerializeObject(table);
            return jsonString;
        }

        /// <summary>
        /// 普通List转JSON
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static string ListToJson(IList list)
        {
            DataTable table = ListToDataTable(list);
            return DtToJson(table);
        }

        /// <summary>
        /// list转dataTable
        /// </summary>
        /// <param name="list">List集合</param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 泛型List转JSON
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static string ListToJson<T>(List<T> list)
        {
            return JsonConvert.SerializeObject(list);
        }
        #endregion

        #region JSON 反序列化
        /// <summary>
        /// JSON 的反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="_JSONString"></param>
        /// <returns></returns>
        public static T JSONToObjectJSON<T>(this string _JSONString) where T : class
        {
            return JsonConvert.DeserializeObject<T>(_JSONString);
        }

        /// <summary>
        /// JSON的反序列化的官方版本
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="_JSONString"></param>
        /// <returns></returns>
        public static T JSONToObjectOfficial<T>(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最全合集)

 

 

GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 3 个月前
8c391e04 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐