• asp.net如何将DataSet转换成josn并输出


    public class JsonUtil

    {

    public string ToJson(DataSet dataSet)

    {

    string jsonString = "{";

    foreach (DataTable table in dataSet.Tables)

    {

    jsonString += """" + table.TableName + """:" + ToJson(table) + ",";

    }

    jsonString = jsonString.TrimEnd(',');

    return jsonString + "}";

    }

    public string ToJson(DataTable dt)

    {

    StringBuilder jsonString = new StringBuilder();

    jsonString.Append("[");

    DataRowCollection drc = dt.Rows;

    for (int i = 0; i < drc.Count; i++)

    {

    jsonString.Append("{");

    for (int j = 0; j < dt.Columns.Count; j++)

    {

    string strKey = dt.Columns[j].ColumnName;

    string strValue = drc[i][j].ToString();

    Type type = dt.Columns[j].DataType;

    jsonString.Append("""" + strKey + """:");

    strValue = String.Format(strValue, type);

    jsonString.Append("""" + strValue + """,");

    }

    jsonString.Append("},");

    }

    jsonString.Remove(jsonString.Length - 1, 1);

    jsonString.Append("]");

    return jsonString.ToString();

    }

    }

    复制代码使用JsonUtil ju = new JsonUtil();

    Response.Write(ju.ToJson(ds));

    复制代码如果解析有问题可以把代码中的""""用"""替换

    佛为心,道为骨,儒为表,大度看世界; 技在手,能在身,思在脑,从容过生活; 三千年读史,不外功名利禄; 九万里悟道,终归诗酒田园;
  • 相关阅读:
    性能测试
    Oracle Install logs
    运维服务方案
    对字符串的查找,剪切,替换,提取(正则表达式)
    用java程序模拟网站的登录以及文件批量上传
    HDU_1542_(树状数组)
    HDU_3792_(素数筛+树状数组)
    Codeforces_791_B. Bear and Friendship Condition_(dfs)
    POJ_1125_(dijkstra)
    POJ_1088_(dp)(记忆化搜索)
  • 原文地址:https://www.cnblogs.com/taofx/p/4137261.html
Copyright © 2020-2023  润新知