2015-11-24 25 views
0

我有以下数据的数据集: enter image description here数据集,以JSON使用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#也很好。

+0

第一转换这个d按照您的要求在实体列表中按照序列化。 – Arshad

+0

我的问题不是“如何序列化数据集到JSON”。我的问题指定“使用VB.Net的数据集到JSON”。这意味着我需要编写一个代码,该代码从数据集中获取行并为其分配三个不同的类,然后对其进行序列化。我无法迭代嵌套对象。 – user1640256

+0

使用LINQ或foreach遍历所有行和列,并准备需要序列化的实体列表。 – Arshad

回答

2

你需要有嵌套类象下面这样:

[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); 
+0

谢谢。我已经定义了类结构。对我来说,真正的挑战是迭代数据集并为它们分配属性,然后序列化对象。我无法获得迭代的效果。 – user1640256

+0

编辑代码以从数据集中添加资源类的填充。 –

+0

这就像一个魅力。谢谢。 – user1640256

0

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

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

我的问题不在于如何序列化。我知道newtonsoft可以为我序列化对象。我的问题在于将线性数据转换为嵌套对象。对不起,如果我没有在我的问题中解释清楚。 – user1640256