简体   繁体   English

使用代码优先实体框架格式化序列化JSON

[英]Formatting Serialized JSON With Code-First Entity Framework

I'm using a Web-API GET to return JSON for a Report. 我正在使用Web-API GET返回报告的JSON。

The GET method returns a simple db.Reports.ToList(); GET方法返回一个简单的db.Reports.ToList();

This is the dump of the data I retrieve 这是我检索到的数据的转储

{
        "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="
    }, ... 
}

This is the related Report declaration: 这是相关的报告声明:

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

In this situation, I'd really like to just have the Ids of the various objects contained in the Report class. 在这种情况下,我真的很想在Report类中包含各种对象的ID。 For example, I'd really just like to see: 例如,我真的很想看看:

    "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. Is there a relatively easy way to go about doing this, or would it be in my better interests to just further parse the result I'm seeing already? 有没有相对简单的方法可以进行此操作,或者只是进一步解析我已经看到的结果是否符合我的利益?
  2. Why does EF by default create these objects within the JSON rather than just the foreign keys? 为什么EF默认情况下会在JSON中创建这些对象,而不仅仅是外键?

You can do without specifically creating a new class. 您无需专门创建一个新类即可。 In your ApiController , if you are using the typed return type (in favor of an HttpResponseMessage ), change the List type to IEnumerable<object> and return: 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
    }));

The default Json serializer (Newtonsoft's Json.Net) is smart enough to serialize anonymous object. 默认的Json序列化程序(Newtonsoft的Json.Net)足够聪明,可以序列化匿名对象。 The only unknown in the code above is the behaviour of the TimeUpdated member as it's a byte array. 上面代码中唯一未知的是TimeUpdated成员的行为,因为它是一个字节数组。 You may have to adjust the assignment. 您可能需要调整分配。

I would recommend making a model for displaying the JSON as you want it to be displayed. 我建议您创建一个用于显示JSON的模型。 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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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