0

我正在使用 aWeb-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="
  1. 是否有一种相对简单的方法可以做到这一点,或者进一步解析我已经看到的结果是否符合我的更好利益?
  2. 为什么 EF 默认在 JSON 中创建这些对象而不仅仅是外键?
4

2 回答 2

3

您无需专门创建新类即可。在您的ApiController中,如果您使用类型化的返回类型(支持 an 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成员的行为,因为它是一个字节数组。您可能需要调整分配。

于 2013-06-28T00:25:21.390 回答
1

I would recommend making a model for displaying the JSON as you want it to be displayed. This would be the easiest option.

Something like this should work:

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;
    }
}
于 2013-06-27T18:56:08.470 回答