C#教程之JsonHelper

pk10开奖历史

VB.net 2010 视频教程 VB.net 2010 视频教程 VB.net 2010 视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
pk10开奖历史 > 编程开发 > C#编程 >
  • C#教程之JsonHelper

  • 2019-06-29 15:45 来源:未知

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

也可以在下面链接下载

链接:http://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
    {
        /// <summary>
        ///  实体转json
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="x">实体</param>
        /// <returns></returns>
        public static string EntityToJson<T>(T x)
        {
            string result = "";
            try
            {
                result = JsonConvert.SerializeObject(x);

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

        /// <summary>
        ///  实体集合List转json
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="x">实体</param>
        /// <returns></returns>
        public static string EntityListToJson<T>(List<T> x)
        {
            string result = "";
            try
            {
                result = JsonConvert.SerializeObject(x);
            }
            catch
            {
                result = "";
            }
            return result;
        }

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

        /// <summary>
        ///  json转实体
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="json">json</param>
        /// <returns></returns>
        public static T JsonToEntity<T>(string json)
        {
            T t = default(T);
            try
            {
                t = (T)JsonConvert.DeserializeObject(json, typeof(T));
            }
            catch
            {
                t = default(T);
            }
            return t;
        }

        /// <summary>
        ///  json list转集合List实体
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="json">json</param>
        /// <returns></returns>
        public static List<T> JsonToListEntity<T>(string json)
        {
            List<T> list = null;
            try
            {
                list = (List<T>)JsonConvert.DeserializeObject(json, typeof(List<T>));
            }
            catch
            {
                list = null;
            }
            return list;
        }



        #region 将json转换为DataTable
        /// <summary>
        /// 将json转换为DataTable
        /// </summary>
        /// <param name="strJson">得到的json</param>
        /// <returns></returns>
        public static DataTable JsonToDataTable(string json)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            ArrayList dic = jss.Deserialize<ArrayList>(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<T>(object reqMessageBody)
        {
            throw new NotImplementedException();
        }
        #endregion


    }
}
复制代码

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

在学习的过程中记录一些笔记及经验 如有疑问请咨询http://wpa.qq.com/msgrd?v=3&uin=2861510776&site=qq&menu=yes
相关教程
pk10开奖历史[email protected]@?KsybPu pk10开奖历史_2wLTbC^KU pk10开奖历史_m&EZPQw*% pk10开奖历史_c1nM48yu? pk10开奖历史_ZxmyChfEy pk10开奖历史_J%xfJ70Gj pk10开奖历史_VR3vjOnXm pk10开奖历史_ujebtis8J pk10开奖历史_fO*[email protected]! pk10开奖历史_OAZaq43KT