zoukankan      html  css  js  c++  java
  • JSON 对象互转

    以前写过用反射,转换,后来觉得有很大漏洞,最近发现有人写过这个help类,所以保存下来

    public class JSONHelper
    {
    /// <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 object ObjectToJSON(object obj)
    {
    try
    {
    JsonSerializerSettings jset = new JsonSerializerSettings();
    jset.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    jset.Converters.Add(new IsoDateTimeConverter { DateTimeFormat = "yyyy'/'MM'/'dd' 'HH':'mm':'ss" });
    return JsonConvert.SerializeObject(obj, jset);
    }
    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;
    }
    }

  • 相关阅读:
    20190430-screen、client、offset、scroll等JS中各种宽度距离
    20190423-Vscode与Sass不得不说的秘密(>^ω^<)
    20190422-外部导入CSS样式之link、CSS@import、Sass分音
    20190421-那些年使用过的CSS预处理器(CSS Preprocessor)之Sass and Less
    20190421-那些年使用过的CSS预处理器(CSS Preprocessor)
    20190409-层叠の层叠上下文、层叠水平、层叠顺序、z-index、伪元素层叠
    20190408-规范书写
    20190404-transition、transform转换、animation、媒体查询
    20190402-display展现、float浮动
    20190401-颜色书写
  • 原文地址:https://www.cnblogs.com/JohnnyBao/p/4496865.html
Copyright © 2011-2022 走看看