zoukankan      html  css  js  c++  java
  • DataTableToJson与ObjectToJson<T>

    public static string DataTableToJson(string jsonName, System.Data.DataTable dt)
            {
                System.Text.StringBuilder Json = new System.Text.StringBuilder();
                Json.Append("{\"" + jsonName + "\":[");
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        Json.Append("{");
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            Json.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":\"" + dt.Rows[i][j].ToString() + "\"");
                            if (j < dt.Columns.Count - 1)
                            {
                                Json.Append(",");
                            }
                        }
                        Json.Append("}");
                        if (i < dt.Rows.Count - 1)
                        {
                            Json.Append(",");
                        }
                    }
                }
                Json.Append("]}");
                return Json.ToString();
            }
            public static string ObjectToJson<T>(string jsonName, IList<T> IL)
            {
                System.Text.StringBuilder Json = new System.Text.StringBuilder();
                Json.Append("{\"" + jsonName + "\":[");
                if (IL.Count > 0)
                {
                    for (int i = 0; i < IL.Count; i++)
                    {
                        T obj = Activator.CreateInstance<T>();
                        Type type = obj.GetType();
                        System.Reflection.PropertyInfo[] pis = type.GetProperties();
                        Json.Append("{");
                        for (int j = 0; j < pis.Length; j++)
                        {
                            Json.Append("\"" + pis[j].Name.ToString() + "\":\"" + pis[j].GetValue(IL[i], null) + "\"");
                            if (j < pis.Length - 1)
                            {
                                Json.Append(",");
                            }
                        }
                        Json.Append("}");
                        if (i < IL.Count - 1)
                        {
                            Json.Append(",");
                        }
                    }
                }
                Json.Append("]}");
                return Json.ToString();
            }

    作者:达奇
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    POJ 1611 The Suspects(并查集)
    POJ 2485 Highways(最小生成树Prim算法)
    POJ 1062 昂贵的聘礼(最短路径Dijkstr的变形)
    SDUT 2374 || HDU 1803Cylinder(计算几何求体积)
    HDU 1804 || SDUT 2375 Deli Deli(简单题)
    POJ 3041 Asteroids(二分图的最大匹配)
    HDU 1802 || SDUT 2373 Black and white painting(规律)
    POJ 1035 Spell checker(字符串简单匹配)
    POJ 3080 Blue Jeans(KMP模式匹配)
    js中反斜杠\的一些研究
  • 原文地址:https://www.cnblogs.com/dachie/p/1825698.html
Copyright © 2011-2022 走看看