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

C# convert datatable to JSON Nested array format

我收到这样的回复

{  
   "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

 }


 ]

}

} 

好吧,如果没有任何代码,就更难查明您做错了什么。但下面是 类 会产生你想要的输出

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

这对你有用,

 class Program
        {       
                static void Main(string[] args)
                {            
                    JObject parsed = JObject.Parse(File.ReadAllText("test.json"));
                   var response = JsonConvert.DeserializeObject<Example>(parsed.ToString());
                    var dict =(JObject) parsed["Data"];                
                    Dictionary<string,List<MonthInfo>> dictValues = new Dictionary<string,List<MonthInfo>>();
                    foreach(var itme in dict)
                    {
                        dictValues.Add(itme.Key,JsonConvert.DeserializeObject<List<MonthInfo>>(itme.Value.ToString()));
                    }

                response.Data = dictValues;
                Console.WriteLine(JsonConvert.SerializeObject(response));
                Console.ReadLine();
                }         

        }


        public class MonthInfo
        {
            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 Example
        {
            public string ResponseStatus { get; set; }
            public string ResponseCode { get; set; }
            public string ResponseMessage { get; set; }
            public Dictionary<string, List<MonthInfo>> Data { get; set; }

        }

输出:

{

  "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

      }


    ]

  }

}