所以我在MVC4中創建了一個WebApi項目,並且無法理解JSON輸出。我不完全確定這是否是JSON的預期輸出或者我是否做了不正確的事情。WebApi 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的和這樣的列表。而我在這裏得到一個大對象,而另一個參考是第二個對象。難道我做錯了什麼,我希望這樣的事情(不要擔心它只是試驗數據,因此沒有真正的密碼: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"}]
好吧,我希望對象包含來自導航屬性的數據。本質上就像 - 項目[ID,名稱-FK1 [名稱,位置]]。我想要的所有數據都在對象中,只是它的觀點有點模糊。我在哪裏可以閱讀DTO模型。 – deep
另外,當我查看一個單一的pb_project時,它會按照預期返回結果,但在查看所有pb_projects時不會。一個單一的回報我,{ 「的$ id」: 「1」, 「pb_Priority」:{...}, 「pb_Tasks」:[...], 「PROJECT_ID」:1, 「USER_ID」:1, 「NAME」:「Project1」, 「DESCRIPTION」:「項目1說明」, 「BUDGET」:1000, 「LOCATION」:「USA」, 「START_DATE」:「2012-11-18T00:00:00 」, 「PRIORITY_ID」:1, 「PROJ_SCALE」:空, 「NOTES」:空 } – deep
反正是有關閉JSON.NET通過引用序列化對象? – deep