繁体   English   中英

使用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#也没关系。

您需要具有如下嵌套类:

[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; }
}

在您的类中填充数据后,您可以使用JavaScriptSerializer类,它已经是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

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

// 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);

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

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

http://www.newtonsoft.com/json上使用.NET <> JSON Newtonsofts JSON。 它可以满足您的所有需求!

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

请参阅以下示例: http//blog.maskalik.com/asp-net/json-net-implement-custom-serialization/http://www.newtonsoft.com/json/help/html/CustomJsonConverter.htm

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM