そのため、MVC4 で WebApi プロジェクトを作成しましたが、JSON 出力を理解するのに苦労していました。これが JSON の意図した出力なのか、それとも何か間違ったことをしたのか、完全にはわかりません。
3 つのテーブル (pb_Project、pb_Tasks、および pb_Priority) があります。その関係は次のとおりです。プロジェクトには多くのタスクがあり、各プロジェクトには 1 つの優先度レベルがあります。
エンティティ http://img705.imageshack.us/img705/9294/stackoverflowentities.png
今、私が受け取っている出力はかなり奇妙で、直感的ではありません. これが意図した JSON であるかどうかはわかりません (私はまったく新しいので)。Web サイトを実行してプロジェクトの 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 などを含むプロジェクト (オブジェクト) のリストです。ここでは、1 つの大きなオブジェクトを取得し、別の参照を 2 番目のオブジェクトとして取得します。私は何か間違ったことをしていますか、私はこのようなことを期待します(ただのテストデータなので心配しないでください。実際のパスワードはありません: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"}]