当前位置: 首页 > 工具软件 > JSONHelper > 使用案例 >

C# JsonHelper

云弘壮
2023-12-01

源代码地址https://github.com/junkai-li/NetCoreKevin
基于NET6搭建跨平台WebApi架构支持单点登录、多缓存、自动任务、分布式、多租户、日志、授权和鉴权 、网关 注册与发现、CAP集成事件、领域事件、 docker部署

C# Json转换类 话不多说上代码

环境配置:引用nuget包Newtonsoft.Json

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace DataSynchronism
{
    public class JsonHelper
    {


        /// <summary>
        /// 通过 Key 获取 Value
        /// </summary>
        /// <returns></returns>
        public static string GetValueByKey(string json, string key)
        {
            try
            {
                JObject jo = (JObject)JsonConvert.DeserializeObject(json);

                return jo[key].ToString();
            }
            catch
            {
                throw new Exception(json);
            }
        }




        /// <summary>
        /// DataRow转JSON
        /// </summary>
        /// <param name="row">DataRow</param>
        /// <returns>JSON格式对象</returns>
        public static object DataRowToJSON(DataRow row)
        {
            Dictionary<string, object> dataList = new Dictionary<string, object>();
            foreach (DataColumn column in row.Table.Columns)
            {
                dataList.Add(column.ColumnName, row[column]);
            }

            return ObjectToJSON(dataList);
        }

        /// <summary>
        /// DataRow转对象,泛型方法
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="row">DataRow</param>
        /// <returns>JSON格式对象</returns>
        public static T DataRowToObject<T>(DataRow row)
        {
            return JSONToObject<T>(DataRowToJSON(row).ToString());
        }

        /// <summary>
        /// DataRow转对象,泛型方法
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="table">DataTable</param>
        /// <returns>JSON格式对象</returns>
        public static List<T> DataTableToList<T>(DataTable table)
        {
            return JSONToList<T>(DataTableToJSON(table).ToString());
        }
        /// <summary>
        /// DataRow转对象,泛型方法
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="jsonText">JSON文本</param> 
        /// <returns>JSON格式对象</returns>
        public static List<T> JSONToList<T>(string jsonText)
        {
            return JSONToObject<List<T>>(jsonText);
        }

        /// <summary> 
        /// 对象转JSON 
        /// </summary> 
        /// <param name="obj">对象</param> 
        /// <returns>JSON格式的字符串</returns> 
        public static string ObjectToJSON(object obj)
        {
            try
            {
                JsonSerializerSettings jset = new JsonSerializerSettings();
                jset.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                jset.Converters.Add(new IsoDateTimeConverter { DateTimeFormat = "yyyy'-'MM'-'dd HH':'mm':'ss" });


                // 设置为驼峰命名
                jset.ContractResolver = new CamelCasePropertyNamesContractResolver();

                return JsonConvert.SerializeObject(obj, jset).ToString();
            }
            catch (Exception ex)
            {
                throw new Exception("JSONHelper.ObjectToJSON(): " + ex.Message);
            }
        }
        /// <summary> 
        /// 数据表转JSON 
        /// </summary> 
        /// <param name="dataTable">数据表</param> 
        /// <returns>JSON字符串</returns> 
        public static object DataTableToJSON(DataTable dataTable)
        {
            return ObjectToJSON(dataTable);
        }

        /// <summary> 
        /// JSON文本转对象,泛型方法 
        /// </summary> 
        /// <typeparam name="T">类型</typeparam> 
        /// <param name="jsonText">JSON文本</param> 
        /// <returns>指定类型的对象</returns> 
        public static T JSONToObject<T>(string jsonText)
        {
            try
            {
                return JsonConvert.DeserializeObject<T>(jsonText.Replace("undefined", "null"));
            }
            catch (Exception ex)
            {
                throw new Exception("JSONHelper.JSONToObject(): " + ex.Message);
            }
        }

        /// <summary> 
        /// JSON文本转对象 
        /// </summary> 
        /// <param name="jsonText">JSON文本</param> 
        /// <param name="type">类型</param>
        /// <returns>指定类型的对象</returns> 
        public static object JSONToObject(string jsonText, Type type)
        {
            try
            {
                return JsonConvert.DeserializeObject(jsonText.Replace("undefined", "null"), type);
            }
            catch (Exception ex)
            {
                throw new Exception("JSONHelper.JSONToObject(): " + ex.Message);
            }
        }


        /// <summary>
        /// [{column1:1,column2:2,column3:3},{column1:1,column2:2,column3:3}]
        /// </summary> 
        /// <param name="strJson">Json字符串</param> 
        /// <returns>DataTable</returns>
        public static DataTable JSONToDataTable(string strJson)
        {
            return JsonConvert.DeserializeObject(strJson, typeof(DataTable)) as DataTable;
        }


        /// <summary>
        /// 没有Key的 Json 转 数组List
        /// </summary>
        /// <param name="strJson"></param>
        /// <returns></returns>
        public static List<JToken> JsonToArrayList(string strJson)
        {
            return ((JArray)JsonConvert.DeserializeObject(strJson)).ToList();
        }
    }
}

 类似资料: