WebApi JSON输出

时间:2012-11-20 05:54:06

标签: asp.net json asp.net-web-api

所以我在MVC4中创建了一个WebApi项目,并且无法理解JSON输出。我不完全确定这是否是JSON的预期输出,或者我是否做错了。

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

entities 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等项目(对象)的列表。而在这里我得到一个大对象,另一个ref作为第二个对象。我做错了什么,我会期待这样的事情(不要担心它只是测试数据所以没有真正的密码: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"}]

1 个答案:

答案 0 :(得分:3)

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

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

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