gpt4 book ai didi

c# - Json 对象到模型 MVC

转载 作者:行者123 更新时间:2023-11-30 21:50:14 25 4
gpt4 key购买 nike

这是我的 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:);

我得到的错误是

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.

我正在使用 WebApi,我不确定我在这里做错了什么。任何帮助将不胜感激

更新:

基于 Shyju 和 Habib 的回答,如果我将它反序列化为单个实例,它确实有效,但如果我将 url 更改为

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

并将其反序列化为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);

关于c# - Json 对象到模型 MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36483403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com