Example #1
0
        /// <summary>
        /// 添加总行数集合, 用于easyUI DataGrid绑定数据用
        /// </summary>
        /// <param name="dt">数据集</param>
        /// <param name="totalCount">总行数</param>
        /// <param name="footer"></param>
        /// <returns></returns>
        public static string DataTableToJson(DataTable dt, int totalCount, string footer)
        {
            try
            {
                StringBuilder strJson = new StringBuilder();

                strJson.Append("{\"total\":");
                strJson.Append(totalCount.ToString());
                strJson.Append(",\"rows\":");
                strJson.Append("[");
                foreach (DataRow dr in dt.Rows)
                {
                    strJson.Append(" {");
                    foreach (DataColumn item in dt.Columns)
                    {
                        strJson.Append("\"" + ConvertHepler.ConvertJson(item.ColumnName) + "\":");

                        if (Equals(dr[item.ColumnName], DBNull.Value))
                        {
                            strJson.Append("null");
                        }
                        else if (string.IsNullOrEmpty(dr[item.ColumnName].ToString()))
                        {
                            strJson.Append("\"\"");
                        }
                        else
                        {
                            strJson.Append("\"" + ConvertHepler.ConvertJson(dr[item.ColumnName].ToString()) + "\"");
                        }
                        strJson.Append(",");
                    }
                    strJson = new StringBuilder(strJson.ToString().TrimEnd(','));
                    strJson.Append("},");
                }

                return(strJson.ToString().TrimEnd(',') + "]" + footer + "}");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #2
0
        public static string ListToJson <T>(List <T> _list) where T : new()
        {
            StringBuilder strJson = new StringBuilder();

            strJson.Append("{\"total\":");
            strJson.Append(_list.Count.ToString());
            strJson.Append(",\"rows\":");
            strJson.Append("[");

            Type type = typeof(T);

            foreach (T _t in _list)
            {
                strJson.Append(" {");
                foreach (PropertyInfo pi in type.GetProperties())
                {
                    object _obj = pi.GetValue(_t, null);
                    string _val = "";
                    if (_obj == null)
                    {
                        _val = "";
                    }
                    else if (_obj.GetType() == typeof(DateTime))
                    {
                        _val = Convert.ToDateTime(_obj).ToString("yyyy-MM-dd HH:mm:ss");
                    }
                    else
                    {
                        _val = _obj.ToString();
                    }

                    strJson.Append("\"" + pi.Name + "\":");
                    strJson.Append("\"" + ConvertHepler.ConvertJson(_val) + "\"");
                    strJson.Append(",");
                }
                strJson = new StringBuilder(strJson.ToString().TrimEnd(','));
                strJson.Append("},");
            }

            return(strJson.ToString().TrimEnd(',') + "]}");
        }
Example #3
0
        public static string DictionaryToJson(Dictionary <string, string> list)
        {
            StringBuilder strJson = new StringBuilder();

            strJson.Append("{");
            foreach (KeyValuePair <string, string> item in list)
            {
                if (item.Value != null)
                {
                    if (item.Value.Length > 0)
                    {
                        if (item.Value[0] == '[' || item.Value[0] == '{')
                        {
                            strJson.Append("\"" + item.Key + "\":" + item.Value + ",");
                            continue;
                        }
                    }
                }
                strJson.Append("\"" + item.Key + "\":" + "\"" + ConvertHepler.ConvertJson(item.Value) + "\",");
            }
            strJson = new StringBuilder(strJson.ToString().TrimEnd(','));
            strJson.Append("}");
            return(strJson.ToString());
        }