Return JSON response in array format in ASP.NET MVC 5 C#
Solution 1:
If you need to use strictly the first structure to return data, then the output structure must be:
public class Required_Result
{
[JsonProperty("draw")]
public int Draw { get; set; }
[JsonProperty("recordsTotal")]
public int RecordsTotal { get; set; }
[JsonProperty("recordsFiltered")]
public int RecordsFiltered { get; set; }
[JsonProperty("data")]
public List<List<string>> Data { get; set; }
}
Then I suposse the data you recover from database is in the second format:
public class Doctors_data
{
[JsonProperty("ID")]
public int ID { get; set; }
[JsonProperty("Name")]
public string Name { get; set; }
[JsonProperty("Date")]
public string Date { get; set; }
[JsonProperty("Gender")]
public string Gender { get; set; }
}
public class Resume
{
[JsonProperty("draw")]
public int Draw { get; set; }
[JsonProperty("recordsTotal")]
public int RecordsTotal { get; set; }
[JsonProperty("recordsFiltered")]
public int RecordsFiltered { get; set; }
[JsonProperty("data")]
public List<Doctors_data> Data { get; set; }
}
So you need to transform your data into the required result format data and deserialize
Resume db = new Resume(); //<--- Populate your data
Required_Result result = new Required_Result()
{
Draw = db.Draw,
RecordsTotal = db.RecordsTotal,
RecordsFiltered = db.RecordsFiltered,
Data = db.Data.Where(e => e.ID > 0).Select(item => new List<string> { item.ID.ToString(), item.Name, item.Date, item.Gender }).ToList()
};
string result_string = JsonSerializer.Serialize(result);