2

here is my Json Object

{
  "id":5,
  "url":"http://localhost:8001/api/courses/5",
  "name":"Professional Experience II",
  "duration":"5.0",
  "description":"The course will talk in depth about every thing",
  "tutor": {
             "id":2,
             "email":"[email protected]",
             "userName":"TA",
             "firstName":"T",
             "lastName":"A",
             "gender":0
           },
  "subject":
           {
             "id":2,
             "name":"Science"
           }
  }

and my model for is here

public class CourseModel
    {
        public int Id { get; set; }
        public string Url { get; set; }
        public string Name { get; set; }
        public double Duration { get; set; }
        public string Description { get; set; }
        public TutorModel Tutor { get; set; }
        public SubjectModel Subject { get; set; }
    }

and this is how i am getting this object. I am able to get the jsonObject but not able to Deserialize it to its corresponding model

string url = "http://localhost:8001/api/courses/5";
HttpResponseMessage responseMessage = await client.GetAsync(url);
if (responseMessage.IsSuccessStatusCode)
{
  var responseData = responseMessage.Content.ReadAsStringAsync().Result;        
  var data = JsonConvert.DeserializeObject<List<CourseModel>>(responseData);
  return View(data);
}
return View("Error:);

Error i am getting is

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Learning.Web.Models.CourseModel]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'id', line 1, position 6.

I am using WebApi and I am not sure what i am doing wrong here .. Any help will be appreciated

Updated:

Based on Shyju and Habib answer it does work if i deserialize it to a single instance but if i change the url to

string url = "http://localhost:8001/api/courses";

and deserialize it to List, i get same error, here is the json object

   {  
       "totalCount":32,
       "totalPages":4,
       "prevPageLink":"",
       "nextPageLink":"http://localhost:8001/api/courses?page=1&pageSize=10",
       "results":[  
          {  
             "id":1,
             "url":"http://localhost:8001/api/courses/1",
             "name":"History Teaching Methods 1",
             "duration":3.0,
             "description":"The course will talk in depth about: History Teaching Methods 1",
             "tutor":{  
                "id":1,
                "email":"[email protected]",
                "userName":"A,
                "firstName":"A",
                "lastName":"J",
                "gender":0
             },
             "subject":{  
                "id":1,
                "name":"History"
             }
          },
          {  
             "id":2,
             "url":"http://localhost:8001/api/courses/2",
             "name":"History Teaching Methods 2",
             "duration":5.0,
             "description":"The course will talk in depth about: History Teaching Methods 2",
             "tutor":{  
                "id":1,
                "email":"[email protected]",
                "userName":"AJo",
                "firstName":"A",
                "lastName":"Jo",
                "gender":0
             },
             "subject":{  
                "id":1,
                "name":"History"
             }
          },
          {  
             "id":3,
             "url":"http://localhost:8001/api/courses/3",
             "name":"History Teaching Methods 3",
             "duration":5.0,
             "description":"The course will talk in depth about: History Teaching Methods 3",
             "tutor":{  
                "id":1,
                "email":"[email protected]",
                "userName":"A",
                "firstName":"A",
                "lastName":"J",
                "gender":0
             },
             "subject":{  
                "id":1,
                "name":"History"
             }
          },
          {  
             "id":4,
             "url":"http://localhost:8001/api/courses/4",
             "name":"Professional Experience 1 (Mathematics/Science)",
             "duration":5.0,
             "description":"The course will talk in depth about: Professional Experience 1 (Mathematics/Science)",
             "tutor":{  
                "id":2,
                "email":"[email protected]",
                "userName":"T",
                "firstName":"T",
                "lastName":"A",
                "gender":0
             },
             "subject":{  
                "id":2,
                "name":"Science"
             }
          },
          {  
             "id":5,
             "url":"http://localhost:8001/api/courses/5",
             "name":"Professional Experience 2 (Mathematics/Science)",
             "duration":5.0,
             "description":"The course will talk in depth about: Professional Experience 2 (Mathematics/Science)",
             "tutor":{  
                "id":2,
                "email":"[email protected]",
                "userName":"T",
                "firstName":"T",
                "lastName":"A",
                "gender":0
             },
             "subject":{  
                "id":2,
                "name":"Science"
             }
          },
          {  
             "id":6,
             "url":"http://localhost:8001/api/courses/6",
             "name":"Professional Experience 3 (Mathematics/Science)",
             "duration":5.0,
             "description":"The course will talk in depth about: Professional Experience 3 (Mathematics/Science)",
             "tutor":{  
                "id":2,
                "email":"[email protected]",
                "userName":"T",
                "firstName":"T",
                "lastName":"A",
                "gender":0
             },
             "subject":{  
                "id":2,
                "name":"Science"
             }
          },
          {  
             "id":7,
             "url":"http://localhost:8001/api/courses/7",
             "name":"Geography Teaching Methods 1",
             "duration":5.0,
             "description":"The course will talk in depth about: Geography Teaching Methods 1",
             "tutor":{  
                "id":3,
                "email":"[email protected]",
                "userName":"Ti",
                "firstName":"T",
                "lastName":"W",
                "gender":0
             },
             "subject":{  
                "id":3,
                "name":"Geography"
             }
          },
          {  
             "id":8,
             "url":"http://localhost:8001/api/courses/8",
             "name":"Geography Teaching Methods 2",
             "duration":5.0,
             "description":"The course will talk in depth about: Geography Teaching Methods 2",
             "tutor":{  
                "id":3,
                "email":"[email protected]",
                "userName":"f",
                "firstName":"f",
                "lastName":"f",
                "gender":0
             },
             "subject":{  
                "id":3,
                "name":"Geography"
             }
          },
          {  
             "id":9,
             "url":"http://localhost:8001/api/courses/9",
             "name":"Geography Teaching Methods 3",
             "duration":5.0,
             "description":"The course will talk in depth about: Geography Teaching Methods 3",
             "tutor":{  
                "id":3,
                "email":"[email protected]",
                "userName":"s",
                "firstName":"s",
                "lastName":"s",
                "gender":0
             },
             "subject":{  
                "id":3,
                "name":"Geography"
             }
          },
          {  
             "id":10,
             "url":"http://localhost:8001/api/courses/10",
             "name":"English Education 1",
             "duration":5.0,
             "description":"The course will talk in depth about: English Education 1",
             "tutor":{  
                "id":4,
                "email":"[email protected]",
                "userName":"g",
                "firstName":"g",
                "lastName":"g",
                "gender":0
             },
             "subject":{  
                "id":4,
                "name":"English"
             }
          }
       ]
    }

2 Answers 2

3

Your JSON represent a single instance of CourseModel. You should be deserializing that to a single instance of CourseModel, not a List of those.

This should work.

var data = JsonConvert.DeserializeObject<CourseModel>(responseData);
Sign up to request clarification or add additional context in comments.

11 Comments

It does work for single instance .. but if i change the url to string url = "localhost:8001/api/courses"; and deserialize it to List i get same error .. any idea?
because your json data is for a single instance, not an array of them.
if i dont use parameter i.e. 5 i get different Json object, do i need to display the other Json object also?
So you need to conditionally deserialize to a single instance or a list based on whether you are passing an Id (Ex: 5) in your REST API call.
Yes, exactly. Sorry for not mentioning it earlier .. i updated the question
|
0

I believe you have setup your Tutor and Subject class properly the error is because you are trying to deserialize a single object to a List<T>

DO:

 var data = JsonConvert.DeserializeObject<CourseModel>(responseData);

Also to create related model, you can paste your JSON to http://json2csharp.com/ and get the generated models, the would look like:

public class Tutor
{
    public int id { get; set; }
    public string email { get; set; }
    public string userName { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
    public int gender { get; set; }
}

public class Subject
{
    public int id { get; set; }
    public string name { get; set; }
}

public class CourseModel
{
    public int id { get; set; }
    public string url { get; set; }
    public string name { get; set; }
    public string duration { get; set; }
    public string description { get; set; }
    public Tutor tutor { get; set; }
    public Subject subject { get; set; }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.