简体   繁体   English

在 ASP.NET MVC 5 C# 中以数组格式返回 JSON 响应

[英]Return JSON response in array format in ASP.NET MVC 5 C#

I want to return JSON format from my database using asp.net mvc5 c#.我想使用 asp.net mvc5 c# 从我的数据库中返回 JSON 格式。 I tried a lot of ways but was unable to generate data as per my requirement.我尝试了很多方法,但无法按照我的要求生成数据。 I need the JSON array in this format.我需要这种格式的 JSON 数组。

{
 "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而不是这个,我得到给定格式的 output

{
 "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我的 ASP.NET MVC5 C# 代码如下

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

If you need to use strictly the first structure to return data, then the output structure must be:如果需要严格使用第一个结构体返回数据,那么output结构体必须是:

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

You can use the following method:您可以使用以下方法:

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

Get draw values from UI.从 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 .有了这个答案,您可以将数据发送到 UI 并将其加载到datatable中。

I use this standard in my projects and it works.我在我的项目中使用这个标准并且它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM