C#将datatable转换为JSON嵌套数组格式

时间:2018-02-26 06:25:10

标签: c# asp.net-web-api2

我收到了这样的回复

{  
   "ResponseStatus":"True",
   "ResponseCode":"0",
   "ResponseMessage":"Calendar Details Received!!",

"data":[  

{ 

         "title":"Holiday",
         "titlePopup":"Holiday",
         "color":"#e1e5ec",
         "msgId":0,
         "start":"2018-01-03T00:00:00",
         "end":"2018-01-03T23:00:00",
         "allDay":0,
         "attachment":"",
         "genFile":"",
         "is_Holiday":1
      },
      {  
         "title":"Holiday",
         "titlePopup":"Holiday",
         "color":"#e1e5ec",
         "msgId":0,
         "start":"2018-01-04T00:00:00",
         "end":"2018-01-04T23:00:00",
         "allDay":0,
         "attachment":"",
         "genFile":"",
         "is_Holiday":1
      },
      {  

         "title":"Holiday",
         "titlePopup":"Holiday",
         "color":"#e1e5ec",
         "msgId":0,
         "start":"2018-02-03T00:00:00",
         "end":"2018-02-03T23:00:00",
         "allDay":0,
         "attachment":"",
         "genFile":"",
         "is_Holiday":1
      },
      {  
         "title":"Holiday",
         "titlePopup":"Holiday",
         "color":"#e1e5ec",
         "msgId":0,
         "start":"2018-02-04T00:00:00",
         "end":"2018-02-04T23:00:00",
         "allDay":0,
         "attachment":"",
         "genFile":"",
         "is_Holiday":1
      }
  ]
}

需要获得响应

  

{

 "ResponseStatus": "True",

 "ResponseCode": "0",

 "ResponseMessage": "Calendar Details Received!!",

 "data":
 {

 "Jan": [

 {

 "title": "Holiday",

 "titlePopup": "Holiday",

 "color": "#e1e5ec",

 "msgId": 0,

 "start": "2018-01-03T00:00:00",

 "end": "2018-01-03T23:00:00",

 "allDay": 0,

 "attachment": "",

 "genFile": "",

 "is_Holiday": 1

 },

 {

 "title": "Holiday",

 "titlePopup": "Holiday",

 "color": "#e1e5ec",

 "msgId": 0,

 "start": "2018-01-04T00:00:00",

 "end": "2018-01-04T23:00:00",

 "allDay": 0,

 "attachment": "",

 "genFile": "",

 "is_Holiday": 1

 }

 ],

 "Feb": [


 {
 "title": "Holiday",

 "titlePopup": "Holiday",

 "color": "#e1e5ec",

 "msgId": 0,

 "start": "2018-02-03T00:00:00",

 "end": "2018-02-03T23:00:00",

 "allDay": 0,

 "attachment": "",

 "genFile": "",

 "is_Holiday": 1

 },

 { 

 "title": "Holiday",

 "titlePopup": "Holiday",

 "color": "#e1e5ec",

 "msgId": 0,

 "start": "2018-02-04T00:00:00",

 "end": "2018-02-04T23:00:00",

 "allDay": 0,

 "attachment": "",

 "genFile": "",

 "is_Holiday": 1

 }


 ]

}

} 

1 个答案:

答案 0 :(得分:2)

没有任何代码,更难以确定你做错了什么。但下面是产生你想要的输出的类

public class Month
{
    public string title { get; set; }
    public string titlePopup { get; set; }
    public string color { get; set; }
    public int msgId { get; set; }
    public DateTime start { get; set; }
    public DateTime end { get; set; }
    public int allDay { get; set; }
    public string attachment { get; set; }
    public string genFile { get; set; }
    public int is_Holiday { get; set; }
}

public class Data
{
    public List<Month> Jan { get; set; }
    public List<Month> Feb { get; set; }
}

public class RootObject
{
    public string ResponseStatus { get; set; }
    public string ResponseCode { get; set; }
    public string ResponseMessage { get; set; }
    public Data data { get; set; }
}