c# - 使用 C#/VB.Net 将数据集转换为 JSON

标签 c# json vb.net dataset

我有一个包含以下数据的数据集: enter image description here

我想将此数据转换为以下格式的 JSON:

{
    "Resource": [
        {
            "resourceID": "1",
            "resourceName": "Jonathan",
            "Customer": [
                {
                    "customerID": "1",
                    "firstName": "Alec",
                    "lastName": "Stewart",
                    "Appointments": [
                        {
                            "appointmentID": "1",
                            "startDate": "01-01-2015",
                            "endDate": "01-01-2015"
                        },
                        {
                            "appointmentID": "2",
                            "startDate": "01-01-2015",
                            "endDate":"01-01-2015",
                        }
                    ]
                },
                {
                    "customerID": "2",
                    "firstName": "Chris",
                    "lastName": "Douglas",
                    "Appointments": [
                        {
                            "appointmentID": "3",
                            "startDate": "01-01-2015",
                            "endDate": "01-01-2015",
                        }
                    ]
                }
            ]
        },
        {
            "resourceID": "2",
            "resourceName": "Matthew",
            "Customer": [
                {
                    "customerID": "3",
                    "firstName": "Shirley",
                    "lastName": "Graham",
                    "Appointments": [
                        {
                            "appointmentID": "4",
                            "startDate": "01-01-2015",
                            "endDate": "01-01-2015",
                        },
                        {
                            "appointmentID": "5",
                            "startDate": "01-01-2015",
                            "endDate": "01-01-2015"
                        }
                    ]
                },
                {
                    "customerID": "4",
                    "firstName": "Ricardo",
                    "lastName": "Powell",
                    "Appointments": [
                        {
                            "appointmentID": "6",
                            "startDate": "01-01-2015",
                            "endDate": "01-01-2015"
                        }
                    ]
                }
            ]
        }
    ]
}

我可以在 VB.Net 中使用任何更快的方法将其直接转换为 JSON 吗?我应该创建类并列出并迭代数据集以创建嵌套类的对象然后序列化它还是可以通过不同的方式实现?有人能告诉我将数据集序列化为 JSON 的方法吗?我也可以使用 C#。

最佳答案

如前所述,newtonsoft是一个真正的美女。你可以这样做:

string json = JsonConvert.SerializeObject(yourdataset, Formatting.Indented);

关于c# - 使用 C#/VB.Net 将数据集转换为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33892689/

相关文章:

c# - 32 位隐式转换无法通过通用重载解析

c# - 如何获取成员(member)的全名

c# - 刷新 Json 局部 View

javascript - 将对象转换为多维数组

vb.net - 用VB.NET逐行读取文件

VB.NET 相当于 VBA 函数中的可选 Range 参数

c# - WPF 的跨平台替代品

c# - 是否可以用 C# 以外的语言实现属性?

c# - DbContext 不会保持连接打开以供重复使用

javascript - 无法使用 JSON.parse 解析字符串数组