使用 C#/VB.Net 将数据集设为 JSON

Dataset to JSON using C#/VB.Net

我有一个包含以下数据的数据集:

我想将此数据转换为 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#。

用于 .NET <> JSON Newtonsofts JSON http://www.newtonsoft.com/json。它可以满足您的一切需求!

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

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

如前所述,您可以为此使用 NewtonSoft。但是没有默认行为。要创建更通用的解决方案,您可以为 Newtonsoft 编写自定义 JsonConverter。并为您的项目实现一种干净的方式来序列化或反序列化。

查看示例:http://blog.maskalik.com/asp-net/json-net-implement-custom-serialization/ or http://www.newtonsoft.com/json/help/html/CustomJsonConverter.htm

您需要像下面这样嵌套 class:

[Serializable]
public class Resource
{
    public string resourceID { get; set; }
    public string resourceName { get; set; }
    public List<Customer> Customers { get; set; }
}

public class Customer
{
    public string customerID { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
    public List<Appointment> Appointments { get; set; }
}

public class Appointment
{
    public string appointmentID { get; set; }
    public string startDate { get; set; }
    public string endDate { get; set; }
}

在 class 中填充数据后,您可以使用 JavaScriptSerializer class,它已经是 System.Web.Script.Serialization 的一部分,如下所示:

var resources = new List<Resource>();
//populate resources data here

JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue;
var resourceJSON = serializer.Serialize(resources); //result

//您可以使用以下代码填充资源 class:

// Fill the DataSet
DataSet dataSet = new DataSet("tblResources");
adapter.Fill(dataSet);

//Populate nested class base on DataSet
var resources = new List<Resource>();

string currentResourceID = string.Empty;
string currentCustomerID = string.Empty;
int totalRows = dataSet.Tables[0].Rows.Count;

var resource = new Resource();
var customer = new Customer();
var customers = new List<Customer>();
var appointments = new List<Appointment>();
bool newResource = false;
bool newCustomer = false;

for (int i = 0; i < totalRows; i++)
{
    var resourceID = dataSet.Tables[0].Rows[i]["resourceID"].ToString();
    var resourceName = dataSet.Tables[0].Rows[i]["resourceName"].ToString();
    var customerID = dataSet.Tables[0].Rows[i]["customerID"].ToString();
    var firstName = dataSet.Tables[0].Rows[i]["firstName"].ToString();
    var lastName = dataSet.Tables[0].Rows[i]["lastName"].ToString();
    var appointmentID = dataSet.Tables[0].Rows[i]["appointmentID"].ToString();
    var startDate = dataSet.Tables[0].Rows[i]["startDate"].ToString();
    var endDate = dataSet.Tables[0].Rows[i]["endDate"].ToString();

    if (!currentResourceID.Equals(resourceID))
    {
        currentResourceID = resourceID;

        resource = new Resource()
        {
            resourceID = resourceID,
            resourceName = resourceName
        };

        resources.Add(resource);

        newResource = true;
    }
    else
    {
        newResource = false;
    }

    if (newResource)
    {
        customers = new List<Customer>();

        resource.Customers = customers;
        currentCustomerID = string.Empty;

    }

    if (!currentCustomerID.Equals(customerID))
    {
        currentCustomerID = customerID;

        customer = new Customer()
        {
            customerID = customerID,
            firstName = firstName,
            lastName = lastName
        };


        customers.Add(customer);

        newCustomer = true;
    }
    else
    {
        newCustomer = false;
    }

    if (newCustomer)
    {
        appointments = new List<Appointment>();

        customer.Appointments = appointments;

    }

    var appointment = new Appointment()
    {
        appointmentID = appointmentID,
        startDate = startDate,
        endDate = endDate
    };

    appointments.Add(appointment);

}

//Convert nested class to json
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue;
var resourceJSON = serializer.Serialize(resources);