1

I am new in .net core programming.

I'm having a problem on how to format my Json Object. I want to create a Json object and one of the field of that JSon will handle a Json Object.

I am using MVC Architecture. Thank you

Here is my code.

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

this function is returning this 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 } ] 

I want to return a Json object that is similar to this.

 { 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 } ] } 
4
  • Create a class with Boolean property Success and List<TableModel> property Data. And populate object of that class and return from the method
    – Chetan
    CommentedSep 13, 2018 at 0:52
  • boolean property? how? do you have sample/
    – Novice
    CommentedSep 13, 2018 at 1:06
  • You have to use model class and DTOs
    – PrathapG
    CommentedSep 13, 2018 at 1:12
  • How? Can you please elaborate?
    – Novice
    CommentedSep 13, 2018 at 1:16

1 Answer 1

2

For formatting the response, you could implement your own Dto like below:

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

Then change your method like

 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; } } 
4
  • am I going to add a new class in my model folder for this? public class ResultDto<T> where T : class
    – Novice
    CommentedSep 13, 2018 at 2:28
  • 1
    @Novice Yes. You need to create a new dto
    – Edward
    CommentedSep 13, 2018 at 2:28
  • So I need to create a new controller for that DTO right? or I'll just edit my existing?
    – Novice
    CommentedSep 13, 2018 at 2:40
  • 1
    @Novice it depends on your requirement, if you only need status and data json, modify existing, if you need both status and data and data, create a new method.
    – Edward
    CommentedSep 13, 2018 at 2:45

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.