zoukankan      html  css  js  c++  java
  • C#对象转json字符串和json字符串转对象

    /* 创建者:菜刀居士的博客
     * 创建日期: 2014年09月03号
     * 功能: Json操作
     *
     */

    namespace Net.String.ConsoleApplication
    {
        using System;
        using System.Data;
        using System.Collections;
        using System.ComponentModel;
        using System.Collections.Generic;
        using System.Web.Script.Serialization;
      

        public class JsonHelper
        {
            /// <summary>
            /// 把json字符串转成对象
            /// </summary>
            /// <typeparam name="T">对象</typeparam>
            /// <param name="data">json字符串</param>
            public static T Deserialize<T>(string data)
            {
                System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer();
                return json.Deserialize<T>(data);
            }

            /// <summary>
            /// 把对象转成json字符串
            /// </summary>
            /// <param name="o">对象</param>
            /// <returns>json字符串</returns>
            public static string Serialize(object o)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer();
                json.Serialize(o, sb);
                return sb.ToString();
            }

            /// <summary>
            /// 把DataTable对象转成json字符串
            /// </summary>
            public string ToJson(DataTable dt)
            {
                JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
                ArrayList arrayList = new ArrayList();
                foreach (DataRow dataRow in dt.Rows)
                {
                    Dictionary<string, object> dictionary = new Dictionary<string, object>();
                    foreach (DataColumn dataColumn in dt.Columns)
                    {
                        dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName]);
                    }
                    arrayList.Add(dictionary);
                }
                return javaScriptSerializer.Serialize(arrayList);
            }
        }

        public static class EnumerableExtension
        {
            /// <summary>
            /// 集合加入一个对象
            /// </summary>
            public static IEnumerable<T> Add<T>(this IEnumerable<T> e, T value)
            {
                foreach (var cur in e)
                { yield return cur; }
                yield return value;
            }

            /// <summary>
            /// 把集合转成DataTable
            /// </summary>
            public static DataTable ConvertToDataTable<T>(this IEnumerable<T> enumerable)
            {
                var dataTable = new DataTable();
                foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(typeof(T)))
                {
                    dataTable.Columns.Add(pd.Name, pd.PropertyType);
                }
                foreach (T item in enumerable)
                {
                    var Row = dataTable.NewRow();

                    foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))
                    {
                        Row[dp.Name] = dp.GetValue(item);
                    }
                    dataTable.Rows.Add(Row);
                }
                return dataTable;
            }
        }
    }

  • 相关阅读:
    【转】Android中设置TextView的颜色setTextColor
    【转】android颜色对应的xml配置值
    【转】color颜色十六进制编码大全
    【转】Usage of sendBroadcast()
    【转】Android4.3 蓝牙BLE初步
    【转】android中重复连接ble设备导致的连接后直接返回STATE_DISCONNECTED的解决办法---不错不错,重新连接需要花费很长的时间
    【转】推荐--《Android深入浅出》----不错
    【转】google chrome如何设置主页
    【转】Chrome保存mhtml网页文件的方法 – 无需任何插件,完美!
    【转】Android--广播BroadcastReceiver
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5205092.html
Copyright © 2011-2022 走看看