使用WebAPI控制器序列化复杂的json对象

时间:2016-12-28 22:35:03

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

我是angularjs和ASP.NET webapi的新手。我正在研究我的基表如下的一些要求

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

我为这些表创建了两个模型类(课程和查找)。如何使用webapi Controller方法public HttpResponseMessage getData(...)

支持如下所示的JSon数据

我需要生成的JSON数据,如下所示

    $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" }
        ]
    }
.
.
.
];

请查看此链接以便更好地理解  https://plnkr.co/edit/73oA3rsrre8gqYX9V25W?p=preview

那么如何编写两个方法getData(生成JSon数据)。请任何人帮我在ASP.NET中序列化这个结构

https://plnkr.co/edit/73oA3rsrre8gqYX9V25W?p=preview

1 个答案:

答案 0 :(得分:1)

假设您已使用以下实体对SQL表进行建模:

public class Question
{
    [Key]
    public int CurriculumID { get; set; }

    public string SubjectArea { get; set; }

    public int CourseNumber { get; set; }
}

public class Rating
{
    [Key]
    public int LookupId { get; set; }

    public string LookupValue { get; set; }
}

下一步是定义一个代表您要序列化的结构的视图模型:

public class QuestionViewModel
{
    public int CurriculumID { get; set; }

    public string SubjectArea { get; set; }

    public int CourseNumber { get; set; }

    public IList<RatingViewModel> Answers { get; set; }
}

public class RatingViewModel
{
    public int LookupId { get; set; }

    public string LookupValue { get; set; }
}

然后在您的控制器中,您可以从数据库中获取实体并从中填充视图模型:

public class QuestionsController: ApiController
{
    [HttpGet]
    [Route("api/questions")]
    public IHttpActionResult Get()
    {
        using (var db = new MyDbContext())
        {
            IList<Rating> ratings = db.Ratings.ToList();
            IList<Question> questions = db.Questions.ToList();
            IList<QuestionViewModel> result = questions.Select(q => new QuestionViewModel
            {
                CurriculumID = q.CurriculumID,
                SubjectArea = q.SubjectArea,
                CourseNumber = q.CourseNumber,
                Answers = ratings.Select(r => new RatingViewModel
                {
                    LookupId = r.LookupId,
                    LookupValue = r.LookupValue,
                }).ToList(),
            }).ToList();

            return this.Ok(result);
        }
    }
}