pavany
pavany

Reputation: 219

serialize complex json object using WebAPI controller

I am new to angularjs and ASP.NET webapi. I am working on some requirement which my base tables are as below

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

I have created two model classes (course and lookup) for these tables. How can i genarate JSon data as below in the backed using webapi Controller method public HttpResponseMessage getData(...)

I need the resultant JSON data like as below

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

Please look into the this link for better understanding https://plnkr.co/edit/73oA3rsrre8gqYX9V25W?p=preview

So how can write the two methods getData(to generate JSon data). please can anyone help me to serialize this structure in ASP.NET

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

Upvotes: 2

Views: 808

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

Let's suppose that you have modeled your SQL tables with the following entities:

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; }
}

the next step would be to define a view model that will represent the structure you want to serialize:

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; }
}

and then in your controller you could fetch the entities from your database and populate the view model from them:

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);
        }
    }
}

Upvotes: 1

Related Questions