[英]Formatting Serialized JSON With Code-First Entity Framework
我正在使用Web-API GET
返回報告的JSON。
GET
方法返回一個簡單的db.Reports.ToList();
這是我檢索到的數據的轉儲
{
"Project": {
"Location": {
"LocationId": 7,
"Description": "New York"
},
"Department": {
"DepartmentId": 7,
"Description": "Engineering"
},
"ProjectId": 7,
"Description": "Project_3",
"LocationId": 7,
"DepartmentId": 7
},
"Person": {
"Email": "email@gmail.com",
"FirstName": "John",
"LastName": "Doe",
"IsActive": true
},
"StatusCode": {
"StatusId": 8,
"Description": "Accepted"
},
"ReportId": "d4cddb3f-ea6a-4b0a-9820-19bd8ee43b3a",
"Description": "Report 3",
"RoundTrip": 45.88,
"IsBillable": true,
"StartDate": "2013-06-27T00:00:00",
"EndDate": "2013-06-27T14:36:32.467",
"TimeUpdated": "AAAAAAAAJxM="
}, ...
}
這是相關的報告聲明:
public class Report
{
public Guid ReportId { get; set; }
public string Description { get; set; }
public double RoundTrip { get; set; }
public bool IsBillable { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public virtual Project Project { get; set; }
public byte[] TimeUpdated { get; set; }
public virtual Person Person { get; set; }
public virtual StatusCode StatusCode { get; set; }
}
在這種情況下,我真的很想在Report
類中包含各種對象的ID。 例如,我真的很想看看:
"Project": 7,
"Location": 7,
"Department": 7,
"Person": "email@gmail.com",
"StatusCode": 8,
"ReportId": "d4cddb3f-ea6a-4b0a-9820-19bd8ee43b3a",
"Description": "Report 3",
"RoundTrip": 45.88,
"IsBillable": true,
"StartDate": "2013-06-27T00:00:00",
"EndDate": "2013-06-27T14:36:32.467",
"TimeUpdated": "AAAAAAAAJxM="
您無需專門創建一個新類即可。 在ApiController
,如果使用類型化的返回類型(支持HttpResponseMessage
),則將List類型更改為IEnumerable<object>
並返回:
return db.Reports.Select(r => new {
Project = r.ProjectId;
Location = r.Location.LocationId;
Department = r.Department.DepartmentId;
Person = r.Person.Email;
StatusCode = r.StatusCode.StatusId;
Description: r.Description
RoundTrip: r.RoundTrip
IsBillable: r.IsBillable,
StartDate: r.StartDate,
EndDate: r.EndDate
TimeUpdated: r.TimeUpdated
});
// Or if you're using HttpResponseMessage
return Request.CreateResponse(HttpStatusCode.Ok,
db.Reports.Select(r => new {
Project = r.ProjectId;
Location = r.Location.LocationId;
Department = r.Department.DepartmentId;
Person = r.Person.Email;
StatusCode = r.StatusCode.StatusId;
Description: r.Description
RoundTrip: r.RoundTrip
IsBillable: r.IsBillable,
StartDate: r.StartDate,
EndDate: r.EndDate
TimeUpdated: r.TimeUpdated
}));
默認的Json序列化程序(Newtonsoft的Json.Net)足夠聰明,可以序列化匿名對象。 上面代碼中唯一未知的是TimeUpdated
成員的行為,因為它是一個字節數組。 您可能需要調整分配。
我建議您創建一個用於顯示JSON的模型。 這將是最簡單的選擇。
這樣的事情應該起作用:
public class ReportSimple
{
public Guid ReportId { get; set; }
public int Project { get; set; }
public int Location { get; set; }
public int Department { get; set; }
public string Person { get; set; }
public int StatusCode { get; set; }
public string Description { get; set; }
public double RoundTrip { get; set; }
public bool IsBillable { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public byte[] TimeUpdated { get; set; }
public ReportSimple(Project project, Person person, StatusCode statusCode)
{
Project = project.ProjectId;
Location = project.Location.LocationId;
Department = project.Department.DepartmentId;
Person = person.Email;
StatusCode = statusCode.StatusId;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.