2

所以我在 MVC4 中创建了一个 WebApi 项目,但无法理解 JSON 输出。我不完全确定这是 JSON 的预期输出还是我做错了什么。

我有三个表(pb_Project、pb_Tasks 和 pb_Priority)。关系如下: 项目可能有很多任务,每个项目可以有一个优先级。

实体 http://img705.imageshack.us/img705/9294/stackoverflowentities.png

现在,我收到的输出很奇怪,而且不是很直观。我不确定这是否是预期的 JSON(因为我是全新的)。当我运行网站以检索项目的 JSON 输出时,我得到以下结果。

[

    {
        "$id": "1",
        "pb_Priority": {
            "$id": "2",
            "pb_Projects": [
                {
                    "$ref": "1"
                },
                {
                    "$id": "3",
                    "pb_Priority": {
                        "$ref": "2"
                    },
                    "pb_Tasks": [
                        {
                            "$id": "4",
                            "pb_Projects": {
                                "$ref": "3"
                            },
                            "TASK_ID": 3,
                            "PROJECT_ID": 2,
                            "USER_ID": 2,
                            "DUE_DATE": "2012-11-19T00:00:00",
                            "PRIORITY_ID": 2,
                            "TASK_NAME": "project 2 task 1",
                            "DESCRIPTION": "asdf"
                        }
                    ],
                    "PROJECT_ID": 2,
                    "USER_ID": 2,
                    "NAME": "Project2",
                    "DESCRIPTION": "project 2 description",
                    "BUDGET": 1,
                    "LOCATION": "CANADA",
                    "START_DATE": "2012-11-18T00:00:00",
                    "PRIORITY_ID": 1,
                    "PROJ_SCALE": null,
                    "NOTES": null
                }
            ],
            "PRIORITY_ID": 1,
            "PRIORITY_LEVEL": 1
        },
        "pb_Tasks": [
            {
                "$id": "5",
                "pb_Projects": {
                    "$ref": "1"
                },
                "TASK_ID": 1,
                "PROJECT_ID": 1,
                "USER_ID": 1,
                "DUE_DATE": "2012-11-19T00:00:00",
                "PRIORITY_ID": 1,
                "TASK_NAME": "project 1 task 1",
                "DESCRIPTION": "adsf"
            },
            {
                "$id": "6",
                "pb_Projects": {
                    "$ref": "1"
                },
                "TASK_ID": 2,
                "PROJECT_ID": 1,
                "USER_ID": 2,
                "DUE_DATE": "2012-11-19T00:00:00",
                "PRIORITY_ID": 1,
                "TASK_NAME": "project 1 task 2 user 2",
                "DESCRIPTION": "asd"
            }
        ],
        "PROJECT_ID": 1,
        "USER_ID": 1,
        "NAME": "Project1",
        "DESCRIPTION": "project 1 description",
        "BUDGET": 1000,
        "LOCATION": "USA",
        "START_DATE": "2012-11-18T00:00:00",
        "PRIORITY_ID": 1,
        "PROJ_SCALE": null,
        "NOTES": null
    },
    {
        "$ref": "3"
    }

]

控制器如下:

public IEnumerable<pb_Projects> Getpb_Projects()
{
    return db.pb_Projects.AsEnumerable();
}

我希望从这个 json 结果中得到一个项目(对象)列表以及任何其他相关的 FK 等。而在这里我得到一个大对象,另一个参考作为第二个对象。我做错了什么吗,我会期待这样的事情(不要担心它只是测试数据,所以没有真正的密码:P)......

[

{
    "$id": "1",
    "UserId": 1,
    "CreateDate": "2012-11-18T21:46:56.78",
    "ConfirmationToken": null,
    "IsConfirmed": true,
    "LastPasswordFailureDate": null,
    "PasswordFailuresSinceLastSuccess": 0,
    "Password": "AIGvGbJIedgTyY+m08DCVhZbVnRTWzW6kvl1JMOqsFqOCSjnJTBlGUGmVveAYIONJw==",
    "PasswordChangedDate": "2012-11-18T21:46:56.78",
    "PasswordSalt": "",
    "PasswordVerificationToken": null,
    "PasswordVerificationTokenExpirationDate": null
},
{
    "$id": "2",
    "UserId": 2,
    "CreateDate": "2012-11-18T21:48:05.067",
    "ConfirmationToken": null,
    "IsConfirmed": true,
    "LastPasswordFailureDate": null,
    "PasswordFailuresSinceLastSuccess": 0,
    "Password": "ACeoUpOVNEvgumvmSkFziRkNQzTlZC8+KAy32egXQseKLmhmJGX/VDXRXylkBUph3Q==",
    "PasswordChangedDate": "2012-11-18T21:48:05.067",
    "PasswordSalt": "",
    "PasswordVerificationToken": null,
    "PasswordVerificationTokenExpirationDate": null
},
{
    "$id": "3",
    "UserId": 3,
    "CreateDate": "2012-11-19T00:35:49.163",
    "ConfirmationToken": null,
    "IsConfirmed": true,
    "LastPasswordFailureDate": null,
    "PasswordFailuresSinceLastSuccess": 0,
    "Password": "AF7W+1FNLcaA2qJ3nmrB7S9mkJmJGZCpAZYbyH5+iXryq1epr8i67ddgSbvFQF1bQw==",
    "PasswordChangedDate": "2012-11-19T00:35:49.163",
    "PasswordSalt": "",
    "PasswordVerificationToken": null,
    "PasswordVerificationTokenExpirationDate": null
},
{
    "$id": "4",
    "UserId": 4,
    "CreateDate": "2012-11-19T00:39:00.667",
    "ConfirmationToken": null,
    "IsConfirmed": true,
    "LastPasswordFailureDate": null,
    "PasswordFailuresSinceLastSuccess": 0,
    "Password": "ABPdfSj8jZAWqhHMDuTVQuL3AiEmiplSujA/GNjQoGoMQm0zhNwAAOaInbqrXFrM0g==",
    "PasswordChangedDate": "2012-11-19T00:39:00.667",
    "PasswordSalt": "",
    "PasswordVerificationToken": null,
    "PasswordVerificationTokenExpirationDate": null
}

]

这是未格式化的原始 JSON

[{"$id":"1","pb_Priority":{"$id":"2","pb_Projects":[{"$ref":"1"},{"$id":"3","pb_Priority":{"$ref":"2"},"pb_Tasks":[],"PROJECT_ID":2,"USER_ID":2,"NAME":"test2","DESCRIPTION":"test2","BUDGET":1.0,"LOCATION":"test","START_DATE":"2012-11-18T00:00:00","PRIORITY_ID":1,"PROJ_SCALE":null,"NOTES":null}],"PRIORITY_ID":1,"PRIORITY_LEVEL":1},"pb_Tasks":[{"$id":"4","pb_Projects":{"$ref":"1"},"TASK_ID":1,"PROJECT_ID":1,"USER_ID":1,"DUE_DATE":"2012-11-19T00:00:00","PRIORITY_ID":1,"TASK_NAME":"asdf","DESCRIPTION":"adsf"},{"$id":"5","pb_Projects":{"$ref":"1"},"TASK_ID":2,"PROJECT_ID":1,"USER_ID":2,"DUE_DATE":"2012-11-19T00:00:00","PRIORITY_ID":1,"TASK_NAME":"sdf","DESCRIPTION":"asd"},{"$id":"6","pb_Projects":{"$ref":"1"},"TASK_ID":3,"PROJECT_ID":1,"USER_ID":2,"DUE_DATE":"2012-11-19T00:00:00","PRIORITY_ID":2,"TASK_NAME":"dsf","DESCRIPTION":"asdf"}],"PROJECT_ID":1,"USER_ID":1,"NAME":"test","DESCRIPTION":"test","BUDGET":null,"LOCATION":"test","START_DATE":"2012-11-18T00:00:00","PRIORITY_ID":1,"PROJ_SCALE":null,"NOTES":null},{"$ref":"3"}]
4

1 回答 1

3

如果您使用的是 EF,默认情况下,它将延迟加载导航属性中的所有数据。当序列化器序列化模型时,它将访问模型上的每个属性,并且 EF 将加载所有嵌套数据。

作为最佳实践,您不应将 DB 模型公开为 Web api 模型。您最好创建另一个 DTO 模型,您可以准确控制将返回给客户端的内容以及您希望从客户端接收的内容。

或者,您可以通过修饰符隐藏这些导航属性(将它们标记为内部)或使用 [IgnoreDataMember] 属性来快速解决此问题。它告诉序列化程序在序列化和反序列化时不要访问它们。

于 2012-11-20T06:57:24.317 回答