• 将DataSet里的数据转换成Json格式


    转:http://www.cnblogs.com/simonblog/archive/2010/05/12/1733139.html

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;

    namespace ConsoleApplication1
    {
        public static class DataSetToJson
        {
            public static string DSToJson(this DataSet ds, string JsonName, string[] ParName)
            {
                try
                {
                    if (ds == null)
                    {
                        return "DataSet Is Null ,So I Can't Do It To Json!";
                    }
                    if (JsonName.Length < 1)
                    {
                        return "You Set The Json Name Is Wrong!";
                    }
                    if (ds.Tables[0].Columns.Count < ParName.Length)
                    {
                        return "You Give The ParName Is Bigger Than DataSet Columns!";
                    }
                    string josn = "{" + JsonName + ":[";
                    string temp = "";
                    for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                    {
                        temp = temp + "{";
                        for (int i = 0; i < ParName.Length; i++)
                        {
                            temp += "" + ParName[i] + ":\'" + ds.Tables[0].Rows[j][ParName[i]] + "\'";
                            if (i != ParName.Length - 1)
                            {
                                temp = temp + ",";
                            }
                       }
                        if (j == ds.Tables[0].Rows.Count - 1)
                        {
                            temp = temp + "}";
                        }
                        else
                        {
                            temp = temp + "},";
                        }
                    }
                    josn = josn + temp + "]}";
                    return josn;
                }
                catch (Exception ex)
                {
                    return "Codeing is Error----" + ex.ToString();
                }
            }
        }
    }

    这是一种对数据取出的数据进行转换成json格式,还有一种就是可以把数据库里的数据存入到一个IList<>里,然后通过json里的JsonWriter来转换成json格式,这里就不显示代码了,,

  • 相关阅读:
    《Java架构师的第一性原理》24Java基础之并发第5篇Java并发编程的艺术
    《Java架构师的第一性原理》71场景题之搜索引擎ElasticSearch
    70道HR常问面试题,找工作避坑必看
    《Java架构师的第一性原理》10计算机基础之计算机组成原理
    《Java架构师的第一性原理》00计算机的第一性原理
    photoshop--历史记录画笔工具-- 所画之处恢复原图像
    photoshop--去色--彩色图像变成灰度图像
    photoshop--历史记录
    qt5-循环遍历语句foreach
    qt5串口通信
  • 原文地址:https://www.cnblogs.com/KimhillZhang/p/1736250.html
Copyright © 2020-2023  润新知