zoukankan      html  css  js  c++  java
  • Datatable 生成json格式

    
    
    public string GetJsonFromDataTable(DataTable dt, int total, bool ShowFooter, string fields, string inputfiled, string ShowMessage = "合计:", string filter = "")
    		{
    			StringBuilder stringBuilder = new StringBuilder();
    			if (dt.Rows.Count == 0)
    			{
    				stringBuilder.Append("{ ");
    				stringBuilder.Append(""rows":[ ");
    				stringBuilder.Append("]");
    				stringBuilder.Append(",");
    				stringBuilder.Append(""total":");
    				stringBuilder.Append(total);
    				stringBuilder.Append(","footer":[");
    				stringBuilder.Append("]");
    				stringBuilder.Append("}");
    				return stringBuilder.ToString();
    			}
    			stringBuilder.Append("{ ");
    			stringBuilder.Append(""rows":[ ");
    			for (int i = 0; i < dt.Rows.Count; i++)
    			{
    				stringBuilder.Append("{ ");
    				for (int j = 0; j < dt.Columns.Count; j++)
    				{
    					if (j < dt.Columns.Count - 1)
    					{
    						stringBuilder.Append(string.Concat(new string[]
    						{
    							""",
    							dt.Columns[j].ColumnName.ToString().ToLower(),
    							"":"",
    							this.JsonCharFilter(dt.Rows[i][j].ToString()),
    							"","
    						}));
    					}
    					else
    					{
    						if (j == dt.Columns.Count - 1)
    						{
    							stringBuilder.Append(string.Concat(new string[]
    							{
    								""",
    								dt.Columns[j].ColumnName.ToString().ToLower(),
    								"":"",
    								this.JsonCharFilter(dt.Rows[i][j].ToString()),
    								"""
    							}));
    						}
    					}
    				}
    				if (i == dt.Rows.Count - 1)
    				{
    					stringBuilder.Append("} ");
    				}
    				else
    				{
    					stringBuilder.Append("}, ");
    				}
    			}
    			stringBuilder.Append("]");
    			stringBuilder.Append(",");
    			stringBuilder.Append(""total":");
    			stringBuilder.Append(total);
    			if (ShowFooter && fields.Length > 0 && inputfiled.Length > 0)
    			{
    				stringBuilder.Append(","footer":[{");
    				stringBuilder.Append(string.Concat(new string[]
    				{
    					""",
    					inputfiled,
    					"":"<span style='color:red;  font-weight:bold'>",
    					ShowMessage,
    					"</span>","
    				}));
    				string[] array = fields.Split(new char[]
    				{
    					','
    				});
    				for (int k = 0; k < array.Length; k++)
    				{
    					if (k < array.Length - 1)
    					{
    						stringBuilder.Append(string.Concat(new object[]
    						{
    							""",
    							array[k],
    							"":"",
    							dt.Compute("sum(" + array[k] + ")", filter),
    							"","
    						}));
    					}
    					else
    					{
    						stringBuilder.Append(string.Concat(new object[]
    						{
    							""",
    							array[k],
    							"":"",
    							dt.Compute("sum(" + array[k] + ")", filter),
    							"""
    						}));
    					}
    				}
    				stringBuilder.Append("}]");
    			}
    			stringBuilder.Append("}");
    			return stringBuilder.ToString();
    		}
    

      

  • 相关阅读:
    90. 子集 II
    274. H 指数
    1291. 顺次数
    StreamWriter 跟BinaryWriter的区别
    非常有用的正则表达式
    linux菜单恢复
    VS2008的诡异问题
    一些常用算法 练手的的代码
    string 类型为空的处理方法
    left jion 的多表连接用法。
  • 原文地址:https://www.cnblogs.com/zhang9418hn/p/3718641.html
Copyright © 2011-2022 走看看