C# 建模MVC的Json对象

C# 建模MVC的Json对象,c#,json,asp.net-mvc,serialization,C#,Json,Asp.net Mvc,Serialization,这是我的Json对象 { "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"

这是我的Json对象

{
  "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":"TA@mymail.com",
             "userName":"TA",
             "firstName":"T",
             "lastName":"A",
             "gender":0
           },
  "subject":
           {
             "id":2,
             "name":"Science"
           }
  }
我的模特儿在这里

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; }
    }
我就是这样得到这个物体的。我能够获取jsonObject,但无法将其反序列化为相应的模型

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:);
并将其反序列化为List,我得到相同的错误,这是json对象

   {  
       "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":"h@outlook.com",
                "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":"A.Jo@fg.com",
                "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":"A@outlook.com",
                "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":"T.A@mymail.com",
                "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":"TA@mymail.com",
                "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":"TA@mymail.com",
                "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":"TW@mymail.com",
                "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":"p@mymail.com",
                "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":"s.s@mymail.com",
                "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":"g.g@outlook.com",
                "userName":"g",
                "firstName":"g",
                "lastName":"g",
                "gender":0
             },
             "subject":{  
                "id":4,
                "name":"English"
             }
          }
       ]
    }

您的JSON表示
CourseModel
的单个实例。您应该将其反序列化为
CourseModel
的单个实例,而不是这些实例的列表

这应该行得通

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

我相信您已经正确地设置了
导师
主题
错误是因为您试图将单个对象反序列化到
列表

做:


它只适用于单个实例。。但如果我将url更改为字符串url=“”;并反序列化它以列出我得到的相同错误。。你知道吗?因为你的json数据是针对单个实例的,而不是它们的数组。如果我不使用参数,即5,我会得到不同的json对象,我是否还需要显示另一个json对象?因此,你需要有条件地反序列化到单个实例或列表,这取决于你是否在REST API调用中传递Id(例如:5)。是的,完全正确。对不起,没有早点提。。我更新了问题
var data = JsonConvert.DeserializeObject<CourseModel>(responseData);
 var data = JsonConvert.DeserializeObject<CourseModel>(responseData);
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; }
}