C#将数据表转换为JSON嵌套数组格式
我得到的回应是这样的C#将数据表转换为JSON嵌套数组格式,c#,asp.net-web-api2,C#,Asp.net Web Api2,我得到的回应是这样的 { "ResponseStatus":"True", "ResponseCode":"0", "ResponseMessage":"Calendar Details Received!!", "data":[ { "title":"Holiday", "titlePopup":"Holiday", "color":"#e1e5ec", "msgId":0,
{
"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
}
]
}
需要得到的答复如下
如果没有任何代码,就很难找出你做错了什么。但下面是生成您想要的输出的类
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; }
}
公共课月
{
公共字符串标题{get;set;}
公共字符串titlePopup{get;set;}
公共字符串颜色{get;set;}
public int msgId{get;set;}
公共日期时间开始{get;set;}
公共日期时间结束{get;set;}
全天公共整数{get;set;}
公共字符串附件{get;set;}
公共字符串genFile{get;set;}
公共int是{get;set;}
}
公共类数据
{
公共列表Jan{get;set;}
公共列表Feb{get;set;}
}
公共类根对象
{
公共字符串ResponseStatus{get;set;}
公共字符串响应代码{get;set;}
公共字符串响应消息{get;set;}
公共数据数据{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; }
}
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
}
]
}
}