在asp.net中无法将DataSet转换为JSON [英] Converting DataSet to JSON is not working in asp.net

查看:197
本文介绍了在asp.net中无法将DataSet转换为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将数据带入许多Table中,因此我希望它在返回数据时将其转换为json.所以我尝试如下

I am bring data in many Table so I want it to convert it to json while returning it. So I tried like below

public static string DataSetToJSON(DataSet dset)
    {
        JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
        List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
        Dictionary<string, object> childRow;
        foreach (DataRow row in dset.Rows)
        {
            childRow = new Dictionary<string, object>();
            foreach (DataColumn col in dset.Columns)
            {
                childRow.Add(col.ColumnName, row[col]);
            }
            parentRow.Add(childRow);
        }
        return jsSerializer.Serialize(parentRow);
    }

但是我在dset.Rows

System.data.dataset不包含Rows ....

推荐答案

在检查并进行了一些研究之后,并且还在SLAKS的指导下,我终于做到了.

After checking and doing some research and also on the guidance of SLAKS I finally did it.

public static string DataSetToJSON(DataSet ds)
    {
        JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
        List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
        Dictionary<string, object> childRow;

        foreach (DataTable table in ds.Tables)
        {
            foreach (DataRow dr in table.Rows)
            {
                childRow = new Dictionary<string, object>();
                foreach (DataColumn col in table.Columns)
                {
                    childRow.Add(col.ColumnName, dr[col]);
                }
                parentRow.Add(childRow);
            }
        }

        return jsSerializer.Serialize(parentRow);
    }

感谢 SLAKS

这篇关于在asp.net中无法将DataSet转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆