skip to Main Content

I want to return JSON format from my database using asp.net mvc5 c#. I tried a lot of ways but was unable to generate data as per my requirement. I need the JSON array in this format.

{
 "draw":0,
 "recordsTotal":2,
 "recordsFiltered":2, 
 "data":[
          [
            "126",
            "Test Name 1",
            "07.01.2022 11:55 AM",
            "Male"
          ],
          [
            "127",
            "Test Name 2",
            "01.02.2022 11:55 AM",
            "Male"
          ]
       ]
}

Instead of this I am getting output in given format

{
 "draw":0,
 "recordsTotal":2,
 "recordsFiltered":2, 
 "data":[
          {
            "ID":126,
            "Name":"Test Name 1",
            "Date":"07.01.2022 11:55 AM",
            "Gender":"Male"
          },
          {
            "ID":127,
            "Name":"Test Name 2",
            "Date":"01.02.2022 11:55 AM",
            "Gender":"Male"
          }
       ]
}

My ASP.NET MVC5 C# code is below

public ContentResult GetDoctor()
        {
            var doctors = db.Doctors.Where(e => e.ID > 0);
            var doct = doctors.Select(c => new
            {
                ID = c.ID + "," +
                "," + c.Name +
                "," + c.Date +
                "," + c.Gender
            }).ToList();
            string students = string.Join("],[", doct.Select(e => e.ID.ToString()));
            students = JsonConvert.SerializeObject(students);
            JsonSerializerSettings hg = new JsonSerializerSettings();
            hg.Formatting = Formatting.Indented;
            hg.TypeNameHandling = TypeNameHandling.None;
            string json = JsonConvert.SerializeObject(doctors.ToList(),hg);
            return Content(json.ToString(), "application/json");
        }

2

Answers


  1. You can use the following method:

    return Json(new
                {
                    draw = draw,
                    recordsFiltered = recordsTotal,
                    recordsTotal = recordsTotal,
                    data = doctors 
                });
    

    Get draw values from UI. like this :

    var draw = Request.Form.GetValues("draw").FirstOrDefault();
    

    The doctors is your list.

    The value of the record number is also obtained from the number of data in the list.

    With this answer, you can send your data to the UI and load it into the datatable.

    I use this standard in my projects and it works.

    Login or Signup to reply.
  2. 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);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search