【发布时间】:2019-02-17 16:28:33
【问题描述】:
我是 .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
}
]
}
【问题讨论】:
-
使用布尔属性 Success 和 List
属性 Data 创建一个类。并填充该类的对象并从方法返回 -
布尔属性?如何?你有样品吗/
-
你必须使用模型类和 DTO
-
怎么样?能详细点吗?
标签: c# json api .net-core asp.net-core-mvc