JSON序列化与反序列化DataSet

本文介绍了如何使用JSON进行DataSet的序列化和反序列化操作。首先,详细阐述了创建DataSet对象并进行序列化的步骤,展示了序列化后的结果。接着,讨论了如何将JSON数据反序列化回DataSet,同样给出了反序列化后的效果。同时,提供了JSON源代码的下载链接以供参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、序列化一个DataSet

1.首先创建一个DataSet对象.

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

namespace JSONDemo
{
    public class DataSetClass
    {
        public DataSet MyDataSet()
        {
            DataSet myDataSet = new DataSet();
            myDataSet.Namespace = "GongHuiJson";

            DataTable table1 = new DataTable();
            DataColumn idColumn = new DataColumn("id", typeof(int));
            idColumn.AutoIncrement = true;
            idColumn.AutoIncrementSeed = 1;
            idColumn.AutoIncrementStep = 2;
            table1.Columns.Add(idColumn);
            table1.Columns.Add(new DataColumn("name", typeof(string)));

            for (int i = 0; i < 3; i++)
            {
                DataRow newRow = table1.NewRow();
                newRow[1] = "indexName" + i;
                table1.Rows.Add(newRow);
            }
            myDataSet.Tables.Add(table1);

            DataTable table2 = new DataTable();
            table2.Columns.Add(new DataColumn("animal", typeof(string)));
            table2.Columns.Add(new DataColumn("voice", typeof(string)));

            DataRow row1 = table2.NewRow();
            row1["animal"] = "cat";
            row1["voice"] = "miaow";
            table2.Rows.Add(row1);

            DataRow row2 = table2.NewRow();
            row2["animal"] = "dog";
            row2["voice"] = "wang";
            table2.Rows.Add(row2);

            myDataSet.Tables.Add(table2);

            return myDataSet;
        }
    }
}


2.序列化DataSet

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GongHuiNewtonsoft.Json;
using GongHuiNewtonsoft.Json.Converters;

namespace JSONDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            DataSetClass ds = new DataSetClass();

            string json = JsonConvert.SerializeObject(ds.MyDataSet(), Formatting.Indented);
            Console.WriteLine(json);
        }
    }
}


3.序列后的结果

\

 

二、JSON反序列化DataSet

1.反序列化DataSet

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using GongHuiNewtonsoft.Json;
using GongHuiNewtonsoft.Json.Converters;

namespace JSONDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string jsonDataSet = @"
            {
                'Table1':
                    [
                        {
                            'id':1,
                            'name':'indexName0'
                        },
                        {
                            'id':3,
                            'name':'indexName1'
                        },
                        {
                            'id':5,
                            'name':'indexName2'
                        }
                    ],
                'Table2':
                    [
                        {
                            'animal':'cat',
                            'voice':'miaow'
                        },
                        {
                            'animal':'dog',
                            'voice':'wang'
                        }
                    ]
            }";

            DataSet myDs = JsonConvert.DeserializeObject<DataSet>(jsonDataSet);

            Console.WriteLine("=========Table1==========");
            DataTable table1 = myDs.Tables["Table1"];
            foreach (DataRow row in table1.Rows)
            {
                Console.WriteLine(row[0] + "/" + row[1]);
            }

            Console.WriteLine("==========Table2=========");
            DataTable table2 = myDs.Tables["Table2"];
            foreach (DataRow row in table2.Rows)
            {
                Console.WriteLine(row["animal"] + "-" + row["voice"]);
            }
        }
    }
}

 

2.反序列后的结果


 

JSON源代码下载地址:http://download.youkuaiyun.com/detail/lovegonghui/9342751

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值