JsonHelper

using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

public class JsonHelper
{
    public static string DataTableToJson(DataTable dt)
    {
        StringBuilder Json = new StringBuilder();
        if (dt != null)
        {
            int row = dt.Rows.Count;
            if (row > 0)
            {
                Json.Append("[");
                for (int i = 0; i < row; i++)
                {
                    Json.Append("{");
                    for (int j = 0, l = dt.Columns.Count; j < l; j++)
                    {
                        Json.Append("\"" + dt.Columns[j].ColumnName + "\":\"" + Convert.ToString(dt.Rows[i][j]) + "\"");
                        if (j < (l - 1))
                        {
                            Json.Append(",");
                        }
                    }
                    Json.Append("}");

                    if (i < (row - 1))
                    {
                        Json.Append(",");
                    }
                }
                Json.Append("]");
            }
        }

        return Json.ToString();
    }

    public static string DataTableToJson(string jsonName, DataTable dt)
    {
        StringBuilder Json = new StringBuilder();
        if (dt != null)
        {
            int row = dt.Rows.Count;
            if (row > 0)
            {
                Json.Append("{\"" + jsonName + "\":[");
                for (int i = 0; i < row; i++)
                {
                    Json.Append("{");
                    for (int j = 0, l = dt.Columns.Count; j < l; j++)
                    {
                        Json.Append("\"" + dt.Columns[j].ColumnName + "\":\"" + Convert.ToString(dt.Rows[i][j]) + "\"");
                        if (j < (l - 1))
                        {
                            Json.Append(",");
                        }
                    }
                    Json.Append("}");

                    if (i < (row - 1))
                    {
                        Json.Append(",");
                    }
                }
                Json.Append("]}");
            }
        }

        return Json.ToString();
    }

    public static string ObjectToJson<T>(IList<T> il) where T : IEntity
    {
        StringBuilder Json = new StringBuilder();
        int ilCount = il.Count;
        if (ilCount > 0)
        {
            Json.Append("[");

            T obj = default(T);
            Type type = default(Type);
            PropertyInfo[] ps = null;
            for (int i = 0; i < ilCount; i++)
            {
                Json.Append("{");
                obj = Activator.CreateInstance<T>();
                type = obj.GetType();
                ps = type.GetProperties();
                for (int j = 0, l = ps.Length; j < l; j++)
                {
                    if (ps[j].Name != "ConnectionName")
                    {
                        Json.Append("\"" + ps[j].Name + "\":\"" + Convert.ToString(ps[j].GetValue(il[i], null)) + "\"");
                        if (j < (l - 1))
                        {
                            Json.Append(",");
                        }
                    }
                }
                Json.Append("}");

                if (i < (ilCount - 1))
                {
                    Json.Append(",");
                }
            }
            Json.Append("]");
        }

        return Json.ToString();
    }

    public static string ObjectToJson<T>(string jsonName, IList<T> il) where T : IEntity
    {
        StringBuilder Json = new StringBuilder();
        int ilCount = il.Count;
        if (ilCount > 0)
        {
            Json.Append("{\"" + jsonName + "\":[");

            T obj = default(T);
            Type type = default(Type);
            PropertyInfo[] ps = null;
            for (int i = 0; i < ilCount; i++)
            {
                Json.Append("{");
                obj = Activator.CreateInstance<T>();
                type = obj.GetType();
                ps = type.GetProperties();
                for (int j = 0, l = ps.Length; j < l; j++)
                {
                    if (ps[j].Name != "ConnectionName")
                    {
                        Json.Append("\"" + ps[j].Name + "\":\"" + Convert.ToString(ps[j].GetValue(il[i], null)) + "\"");
                        if (j < (l - 1))
                        {
                            Json.Append(",");
                        }
                    }
                }
                Json.Append("}");

                if (i < (ilCount - 1))
                {
                    Json.Append(",");
                }
            }
            Json.Append("]}");
        }

        return Json.ToString();
    }

    public static string ObjectToJson<T>(T entity) where T : IEntity, new()
    {
        StringBuilder Json = new StringBuilder();
        if (entity != null)
        {
            Json.Append("{");
            T obj = Activator.CreateInstance<T>();
            Type type = obj.GetType();
            PropertyInfo[] ps = type.GetProperties(); ;

            for (int j = 0, l = ps.Length; j < l; j++)
            {
                if (ps[j].Name != "ConnectionName")
                {
                    Json.Append("\"" + ps[j].Name + "\":\"" + Convert.ToString(ps[j].GetValue(entity, null)) + "\"");
                    if (j < (l - 1))
                    {
                        Json.Append(",");
                    }
                }
            }
            Json.Append("}");
        }

        return Json.ToString();
    }

    public static string ObjectToJson<T>(string jsonName, T entity) where T : IEntity, new()
    {
        StringBuilder Json = new StringBuilder();
        if (entity != null)
        {
            Json.Append("{\"" + jsonName + "\":[{");
            T obj = Activator.CreateInstance<T>();
            Type type = obj.GetType();
            PropertyInfo[] ps = type.GetProperties(); ;

            for (int j = 0, l = ps.Length; j < l; j++)
            {
                if (ps[j].Name != "ConnectionName")
                {
                    Json.Append("\"" + ps[j].Name + "\":\"" + Convert.ToString(ps[j].GetValue(entity, null)) + "\"");
                    if (j < (l - 1))
                    {
                        Json.Append(",");
                    }
                }
            }
            Json.Append("}]}");
        }

        return Json.ToString();
    }

    public static ArrayList GetList(DataTable dt)
    {
        ArrayList arrList = new ArrayList();
        if (dt != null
            && dt.Rows.Count > 0)
        {
            Dictionary<string, object> dic = null;
            foreach (DataRow dr in dt.Rows)
            {
                dic = new Dictionary<string, object>();
                foreach (DataColumn cl in dt.Columns)
                {
                    if (string.Compare("ConnectionName", cl.ColumnName) != 0)
                    {
                        dic.Add(cl.ColumnName, dr[cl]);
                    }
                }
                arrList.Add(dic);
            }
        }
        return arrList;
    }
}

你可能感兴趣的:(C#--Json)