2
我是angularjs和ASP.NET webapi的新手。我工作的一些要求,其中我的基表如下使用WebAPI控制器序列化複雜的json對象
CurriculumID SubjectArea CourseNumber
------------ ----------- ------------
303 GHIJ 101
304 ABCD 102
305 MNPQ 103
306 WXYZ 104
lookupId lookupValue
-------- -----------
1 Very Useful
2 Somewhat Useful
3 Not Useful
4 Not Applicable
5 Elsewhere
我已經創建了這些表兩型類(當然和查找)。我怎樣才能如下genarate JSON數據備份使用的WebAPI控制器的方法public HttpResponseMessage getData(...)
我需要一個像下面
$scope.questions = [
{
"CurriculumID": "303", "SubjectArea": "GHIJ", "CourseNumber": "101", "answers": [
{ "lookupValue": "Very Useful","lookupId":"1" },
{ "lookupValue": "Somewhat Useful", "lookupId": "2" },
{ "lookupValue": "Not Useful", "lookupId": "3" },
{ "lookupValue": "Not Applicable", "lookupId": "4" },
{ "lookupValue": "Elsewhere", "lookupId": "5" }
]
},
{
"CurriculumID": "304", "SubjectArea": "ABCD", "CourseNumber": "102", "answers": [
{ "lookupValue": "Very Useful","lookupId":"1" },
{ "lookupValue": "Somewhat Useful", "lookupId": "2" },
{ "lookupValue": "Not Useful", "lookupId": "3" },
{ "lookupValue": "Not Applicable", "lookupId": "4" },
{ "lookupValue": "Elsewhere", "lookupId": "5" }
]
}
.
.
.
];
請看這個鏈接爲了更好地理解所產生的JSON數據 https://plnkr.co/edit/73oA3rsrre8gqYX9V25W?p=preview
那麼如何編寫兩個方法getData(來生成JSon數據)。請誰能幫助我在ASP.NET序列化此結構
https://plnkr.co/edit/73oA3rsrre8gqYX9V25W?p=preview
非常感謝您的快速響應。我遵循你的程序,但我得到了如下錯誤。 類型'<> f__AnonymousType5'4 [System.String,System.String,System.String,System.Collections.Generic.List'1 [<> f__AnonymousType6'2 [System.Int32,System.String]]]''不能被序列化。考慮使用DataContractAttribute屬性標記它,並使用DataMemberAttribute屬性標記要序列化的所有成員。如果類型是一個集合,請考慮使用CollectionDataContractAttribute來標記它。其他支持 – user2293819
'[Table(「Course」)] public class Course { [Key] public string CurriculumID {get;組; } public string SubjectArea {get;組; } public string CourseNumber {get;組; } }' '[表( 「sp.option_lookup_st_fdbk」)] 公共類option_lookup_st_fdbk { [鍵] 公衆詮釋lookupId所{得到;組; } 公共字符串LookupValue {get;組; } }' – user2293819
and the method 'IList rating = db.option_lookup_st_fdbk.ToList(); IList questions = db.Course.ToList(); VAR結果= questions.Select(Q =>新 { CurriculumID = q.CurriculumID, SubjectArea = q.SubjectArea, CourseNumber = q.CourseNumber, 答案= rating.Select(R =>新 { lookupId所= r.lookupId, LookupValue = r.LookupValue })。ToList(), })。ToList(); return this.Ok(result);' –
user2293819