【问题标题】:C# convert datatable to JSON Nested array formatC# 将数据表转换为 JSON 嵌套数组格式
【发布时间】:2018-02-26 06:25:09
【问题描述】:

我收到这样的回复

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

 }


 ]

}

} 

【问题讨论】:

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


    【解决方案1】:

    这对你有用,

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

    【讨论】:

      【解决方案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; }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-12-17
        • 1970-01-01
        • 1970-01-01
        • 2018-05-15
        • 2021-04-06
        • 1970-01-01
        • 2021-10-29
        • 1970-01-01
        相关资源
        最近更新 更多