简体   繁体   中英

JSon within a JSon .net core

I am new in .net core programming.

I need to return a Json object within a Json object in .net core MVC. I want to return a list schedules and the infos of the teams that belongs on that schedules.

as of now I'm getting this output using this codes.

for SCHEDULES:

code in controller

 [HttpGet]
 public async Task<List<Schedules>> getAllScheds(){
     return await _context.Schedules.ToListAsync();
 }

output

 [
     {
         "scheduleId": 43,
         "teamId1": 1,
         "teamId2": 3,
         "scheduleDate": "2016-04-30T19:00:00",
         "week": "1",
         "stadiumId": 3,
         "createdBy": null,
         "createdDate": "2016-07-07T13:09:32.797"
     },
     {
         "scheduleId": 44,
         "teamId1": 7,
         "teamId2": 6,
         "scheduleDate": "2016-05-01T16:00:00",
         "week": "1",
         "stadiumId": 6,
         "createdBy": null,
         "createdDate": "2016-07-07T13:13:10.183"
     },
     {
         "scheduleId": 45,
         "teamId1": 2,
         "teamId2": 4,
         "scheduleDate": "2016-05-02T22:00:00",
         "week": "1",
         "stadiumId": 4,
         "createdBy": null,
         "createdDate": "2016-07-07T13:16:23.337"
     }
 ]

for Team infos

code in controller

 [HttpGet]
 public async Task<List<Teams>> getAllTeamsInfo(){
     return await _context.Teams.ToListAsync();
 }

output

 [
     {
         "teamId": 1,
         "city": "Virginia",
         "teamName": "Armada",
         "sImage": "/images/teams-logo-small/virginia.png"
         "createdBy": null,
         "createdDate": "2016-06-22T10:03:35.58"
     },
     {
         "teamId": 2,
         "city": "Arkansas",
         "teamName": "Attack",
         "sImage": "/images/teams-logo-small/arkansas.png"
         "createdBy": null,
         "createdDate": "2016-06-22T10:03:35.58"
     },
     {
         "teamId": 3,
         "city": "Florida",
         "teamName": "Fusion",
         "sImage": "/images/teams-logo-small/florida.png"
         "createdBy": null,
         "createdDate": "2016-06-22T10:03:35.58",
         "modifiedBy": null
     }
 ]

Now I want to merge them and have this output .

 [
     {
         "scheduleId": 43,
         "teamId1": [{
                      "teamId": 1,
                      "city": "Virginia",
                      "teamName": "Armada",
                      "sImage": "/images/teams-logo-small/virginia.png"
                      "createdBy": null,
                      "createdDate": "2016-06-22T10:03:35.58"
                     }],
         "teamId2":  [{
                      "teamId": 3,
                      "city": "Florida",
                      "teamName": "Fusion",
                      "sImage": "/images/teams-logo-small/florida.png"
                      "createdBy": null,
                      "createdDate": "2016-06-22T10:03:35.58",
                      "modifiedBy": null
                     }],
         "scheduleDate": "2016-04-30T19:00:00",
         "week": "1",
         "stadiumId": 3,
         "createdBy": null,
         "createdDate": "2016-07-07T13:09:32.797"
     },
     {
         "scheduleId": 44,
         "teamId1":  [{
                      "teamId": 7,
                      "city": "Oklahoma",
                      "teamName": "Nation",
                      "sImage": "/images/teams-logo-small/oklahoma.png",
                      "createdBy": null,
                      "createdDate": "2016-06-22T10:03:35.58"
                     }],
         "teamId2": [{
                      "teamId": 6,
                      "city": "Texas",
                      "teamName": "Independence",
                      "sImage": "/images/teams-logo-small/texas.png",
                      "createdBy": null,
                      "createdDate": "2016-06-22T10:03:35.58"
                    }],
         "scheduleDate": "2016-05-01T16:00:00",
         "week": "1",
         "stadiumId": 6,
         "createdBy": null,
         "createdDate": "2016-07-07T13:13:10.183"
     },
     {
         "scheduleId": 45,
         "teamId1": [{
                      "teamId": 2,
                      "city": "Arkansas",
                      "teamName": "Attack",
                      "sImage": "/images/teams-logo-small/arkansas.png",
                      "createdBy": null,
                      "createdDate": "2016-06-22T10:03:35.58"
                    }],
         "teamId2": [{
                      "teamId": 4,
                      "city": "Oregon",
                      "teamName": "Crash",
                      "sImage": "/images/teams-logo-small/oregon.png",
                      "createdBy": null,
                      "createdDate": "2016-06-22T10:03:35.58",
                    }],
         "scheduleDate": "2016-05-02T22:00:00",
         "week": "1",
         "stadiumId": 4,
         "createdBy": null,
         "createdDate": "2016-07-07T13:16:23.337"
     }
 ]

instead of the team ID, I want to return the whole Team info.

Thank you very much.

If you don't have the Team navigation properties in your Schedules model you have to add them first:

public class Schedules {
    [ForeignKey("Team1Id")]
    public Team Team1 {get;set;}

    [ForeignKey("Team2Id")]
    public Team Team2 {get;set;}
}

Then you can use the Include(s=>s.Team1).ThenInclude(s=>s.Team2) in your query.

An other way is to create a new model that represent the values you want. This will be the result of your controller method. For example:

public class ScheduleDTO:Schedule {
    public Team Team1 {get;set;}
    public Team Team2 {get;set;}
}

In the request you select this new model for each Schedule:

[HttpGet]
public async Task<List<ScheduleDTO>> getAllScheds(){
    return await _context.Schedules.Select(s=>new ScheduleDTO() {
        scheduleId: s.Id,
        teamId1: s.Team1Id,
        teamId2: s.Team2Id,
        scheduleDate: "2016-04-30T19:00:00",
        week: "1",
        stadiumId: 3,
        createdBy: null,
        createdDate: "2016-07-07T13:09:32.797",
        Team1:s.Team1,
        Team2:_context.Teams.FirstOrDefault(x=>x.Id==s.Team1Id) //not recommended
    });
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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