JsonHelper

Json对于一个开发者来说已经不陌生了,首先肯定要引用Newtonsoft.Json,对于开发者可以在NuGet包里面下载

也可以在下面链接下载

链接:https://pan.baidu.com/s/1yH5Yp2c9f73Bkx5bLiiiNQ
提取码:xi64

 

JsonHelper
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Web.Script.Serialization;

namespace Common
{
    public class JsonHelper
    {
        /// 
        ///  实体转json
        /// 
        /// 实体
        /// 实体
        /// 
        public static string EntityToJson(T x)
        {
            string result = "";
            try
            {
                result = JsonConvert.SerializeObject(x);

            }
            catch
            {
                result = "";
            }
            return result;
        }

        /// 
        ///  实体集合List转json
        /// 
        /// 实体
        /// 实体
        /// 
        public static string EntityListToJson(List x)
        {
            string result = "";
            try
            {
                result = JsonConvert.SerializeObject(x);
            }
            catch
            {
                result = "";
            }
            return result;
        }

        /// 
        /// 实体集合list转json_null
        /// 
        /// 实体
        /// 实体
        /// 
        public static string EntityListToJson_null(List x)
        {
            string result = "";
            try
            {
                var jSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
                result = JsonConvert.SerializeObject(x, Formatting.Indented, jSetting);
            }
            catch
            {
                result = "";
            }
            return result;
        }

        /// 
        ///  json转实体
        /// 
        /// 实体
        /// json
        /// 
        public static T JsonToEntity(string json)
        {
            T t = default(T);
            try
            {
                t = (T)JsonConvert.DeserializeObject(json, typeof(T));
            }
            catch
            {
                t = default(T);
            }
            return t;
        }

        /// 
        ///  json list转集合List实体
        /// 
        /// 实体
        /// json
        /// 
        public static List JsonToListEntity(string json)
        {
            List list = null;
            try
            {
                list = (List)JsonConvert.DeserializeObject(json, typeof(List));
            }
            catch
            {
                list = null;
            }
            return list;
        }



        #region 将json转换为DataTable
        /// 
        /// 将json转换为DataTable
        /// 
        /// 得到的json
        /// 
        public static DataTable JsonToDataTable(string json)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            ArrayList dic = jss.Deserialize(json);
            DataTable dtb = new DataTable();
            if (dic.Count > 0)
            {
                foreach (Dictionary<string, object> drow in dic)
                {
                    if (dtb.Columns.Count == 0)
                    {
                        foreach (string key in drow.Keys)
                        {
                            try
                            {
                                dtb.Columns.Add(key, drow[key].GetType());
                            }
                            catch (Exception)
                            {
                                string a="";
                                dtb.Columns.Add(key, a.GetType());
                            } 
                            
                        }
                    } DataRow row = dtb.NewRow();
                    foreach (string key in drow.Keys)
                    { row[key] = drow[key]; }
                    dtb.Rows.Add(row);
                }
            }
            return dtb;
        }
        #endregion
        #region 将DataTable转换为json
        public static string DataTableToJsonWithJsonNet(DataTable table)
        {
            string JsonString = string.Empty;
            JsonString = JsonConvert.SerializeObject(table);
            return JsonString;
        }

        public static T JsonToEntity(object reqMessageBody)
        {
            throw new NotImplementedException();
        }
        #endregion


    }
}

有疑问请咨询https://wpa.qq.com/msgrd?v=3&uin=2861510776&site=qq&menu=yes

你可能感兴趣的:(JsonHelper)