在.net核心MVC中创建JSon

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在.net核心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
         },
         "scheduleDate": "2016-04-30T19:00:00",
         "week": "1",
         "stadiumID": {
             "stadiumId": 3,
             "name": "Orlando Florida (Citrus Bowl)",
             "location": "Florida",
             "capacity": 20000,
             "image": "/images/teams-stadium/Orlando-Flor.png",
             "teamId": 3,
             "createdBy": null,
             "createdDate": "2016-06-22T10:03:28.99",
             "modifiedBy": null,
             "modifiedDate": null
         },
         "createdBy": null,
         "createdDate": "2016-07-07T13:09:32.797",
         "modifiedBy": "user1",
         "modifiedDate": null,
         "gateScheduleOpen": "2016-04-30T19:00:00",
         "seasonId": 1
     },
     {
         "scheduleId": 44,
         "team1": {
             "teamId": 7,
             "city": "Oklahoma",
              "teamName": "Nation",
             "sImage": "/images/teams-logo-small/oklahoma.png",
             "image": "/images/teams-logo/oklahoma.png",
             "helmetImage": "/images/teams-helmet/oklahoma.png",
             "createdBy": null,
             "createdDate": "2016-06-22T10:03:35.58",
             "modifiedBy": null,
             "modifiedDate": null,
             "isDeleted": null
         },
         "team2": {
             "teamId": 6,
             "city": "Texas",
             "teamName": "Independence",
             "sImage": "/images/teams-logo-small/texas.png",
             "image": "/images/teams-logo/texas.png",
             "helmetImage": "/images/teams-helmet/texas.png",
             "createdBy": null,
             "createdDate": "2016-06-22T10:03:35.58",
             "modifiedBy": null,
             "modifiedDate": null,
             "isDeleted": null
         },
         "scheduleDate": "2016-05-01T16:00:00",
         "week": "1",
         "stadiumID": {
             "stadiumId": 6,
             "name": "Austin Texas (Kelly Reaves High School Stadium)",
             "location": "Texas",
             "capacity": 20000,
             "image": "/images/teams-stadium/Texas.png",
             "teamId": 6,
             "createdBy": null,
             "createdDate": "2016-06-22T10:03:28.99",
             "modifiedBy": null,
             "modifiedDate": null
         },
         "createdBy": null,
         "createdDate": "2016-07-07T13:13:10.183",
         "modifiedBy": "user1",
         "modifiedDate": null,
         "gateScheduleOpen": "2016-05-01T16:00:00",
         "seasonId": 1
     }
 ]

我想返回一个类似于此的Json对象。

 {
       Status: "success",
       Data: [{
               "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
              },
              "scheduleDate": "2016-04-30T19:00:00",
              "week": "1",
              "stadiumID": {
                  "stadiumId": 3,
                  "name": "Orlando Florida (Citrus Bowl)",
                  "location": "Florida",
                  "capacity": 20000,
                  "image": "/images/teams-stadium/Orlando-Flor.png",
                  "teamId": 3,
                  "createdBy": null,
                  "createdDate": "2016-06-22T10:03:28.99",
                  "modifiedBy": null,
                  "modifiedDate": null
              },
              "createdBy": null,
              "createdDate": "2016-07-07T13:09:32.797",
              "modifiedBy": "user1",
              "modifiedDate": null,
              "gateScheduleOpen": "2016-04-30T19:00:00",
              "seasonId": 1
          },
          {
              "scheduleId": 44,
              "team1": {
                  "teamId": 7,
                  "city": "Oklahoma",
                  "teamName": "Nation",
                  "sImage": "/images/teams-logo-small/oklahoma.png",
                  "image": "/images/teams-logo/oklahoma.png",
                  "helmetImage": "/images/teams-helmet/oklahoma.png",
                  "createdBy": null,
                  "createdDate": "2016-06-22T10:03:35.58",
                  "modifiedBy": null,
                  "modifiedDate": null,
                  "isDeleted": null
              },
              "team2": {
                  "teamId": 6,
                  "city": "Texas",
                  "teamName": "Independence",
                  "sImage": "/images/teams-logo-small/texas.png",
                  "image": "/images/teams-logo/texas.png",
                  "helmetImage": "/images/teams-helmet/texas.png",
                  "createdBy": null,
                  "createdDate": "2016-06-22T10:03:35.58",
                  "modifiedBy": null,
                  "modifiedDate": null,
                  "isDeleted": null
              },
              "scheduleDate": "2016-05-01T16:00:00",
              "week": "1",
              "stadiumID": {
                  "stadiumId": 6,
                  "name": "Austin Texas (Kelly Reaves High School Stadium)",
                  "location": "Texas",
                  "capacity": 20000,
                  "image": "/images/teams-stadium/Texas.png",
                  "teamId": 6,
                  "createdBy": null,
                  "createdDate": "2016-06-22T10:03:28.99",
                  "modifiedBy": null,
                  "modifiedDate": null
              },
              "createdBy": null,
              "createdDate": "2016-07-07T13:13:10.183",
              "modifiedBy": "user1",
              "modifiedDate": null,
              "gateScheduleOpen": "2016-05-01T16:00:00",
              "seasonId": 1
          }
      ]
 }
答案

要格式化响应,您可以实现自己的Dto,如下所示:

    public class ResultDto<T> where T : class
{
    public ResultDto(string status, IList<T> data)
    {
        Status = status;
        Data = data;
    }
    public string Status { get; set; }
    public IList<T> Data { get; set; }
}

然后改变你的方法

        public async Task<ResultDto<Product>> getAllScheds()
    {
        dynamic response = new JObject();
        try
        {

            var data = new List<Product>
                     {
                         new Product{ProductId=Guid.NewGuid().ToString(),Name="142525"},
                         new Product{ProductId=Guid.NewGuid().ToString(),Name="122555"},
                         new Product{ProductId=Guid.NewGuid().ToString(),Name="125255"}
                     };
            return new ResultDto<Product>("success", data);
        }
        catch (Exception e)
        {
            response.Error = e.ToString();
            return response;
        }

    }

以上是关于在.net核心MVC中创建JSon的主要内容,如果未能解决你的问题,请参考以下文章

如何使用动态对象类创建 .net 核心 MVC 模型?

如何在 asp.net mvc 核心中链接 css?

如何在 asp.net mvc 中创建审计日志/审计跟踪

pbootcms对接微信扫码登录代码核心片段和步骤(前后端)

在 Asp.net Identity MVC 5 中创建角色

如何在 ASP.NET MVC 中创建 CheckBoxListFor 扩展方法?