在 .net core MVC 中创建 JSON

我是 .net 核心编程的新手。


我在如何格式化我的 Json 对象时遇到问题。我想创建一个 Json 对象,并且该 JSON 的字段之一将处理一个 Json 对象。


我正在使用 MVC 架构。谢谢


这是我的代码。


    public async Task<List<Schedule>> getAllScheds(){

        dynamic response = new JObject();

        try {


            var data = await _context.MlfbSchedules

                        .Include(h => h.Team1)

                        .Include(a => a.Team2)

                        .Include(s => s.StadiumID)

                        .ToListAsync();

            return data;

        } catch (Exception e) {

            response.Error = e.ToString();

            return response;

        }


    }

此函数正在返回此数据。


 [

     {

         "scheduleId": 43,

         "team1": {

             "teamId": 1,

             "city": "Virginia",

             "teamName": "Armada",

             "sImage": "/images/teams-logo-small/virginia.png",

             "image": "/images/teams-logo/virginia.png",

             "helmetImage": "/images/teams-helmet/virginia.png",

             "createdBy": null,

             "createdDate": "2016-06-22T10:03:35.58",

             "modifiedBy": null,

             "modifiedDate": null,

             "isDeleted": null

         },

         "team2": {

             "teamId": 3,

             "city": "Florida",

             "teamName": "Fusion",

             "sImage": "/images/teams-logo-small/florida.png",

             "image": "/images/teams-logo/florida.png",

             "helmetImage": "/images/teams-helmet/florida.png",

             "createdBy": null,

             "createdDate": "2016-06-22T10:03:35.58",

             "modifiedBy": null,

             "modifiedDate": null,

             "isDeleted": null

         },


慕田峪7331174
浏览 331回答 1
1回答

慕丝7291255

为了格式化响应,您可以实现自己的 Dto,如下所示:&nbsp; &nbsp; public class ResultDto<T> where T : class{&nbsp; &nbsp; public ResultDto(string status, IList<T> data)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Status = status;&nbsp; &nbsp; &nbsp; &nbsp; Data = data;&nbsp; &nbsp; }&nbsp; &nbsp; public string Status { get; set; }&nbsp; &nbsp; public IList<T> Data { get; set; }}然后改变你的方法&nbsp; &nbsp; &nbsp; &nbsp; public async Task<ResultDto<Product>> getAllScheds()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; dynamic response = new JObject();&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var data = new List<Product>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;new Product{ProductId=Guid.NewGuid().ToString(),Name="142525"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;new Product{ProductId=Guid.NewGuid().ToString(),Name="122555"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;new Product{ProductId=Guid.NewGuid().ToString(),Name="125255"}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new ResultDto<Product>("success", data);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (Exception e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.Error = e.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return response;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP