c# - 将 JSON 反序列化为对象列表 (RestSharp)
问题描述
我似乎无法弄清楚为什么每次我执行 GET 请求并尝试将数据从 JSON 反序列化到对象列表时它都没有值(NULL)。如果我输出 response.content 我得到整个 JSON(在尝试反序列化之前)。
{
"Information": [
{
"Name": "Samuel",
"ID": "samJ",
"Sales": "5-A",
"FoodChoice": [
"Hamburger",
"Hotdog"
]
},
{
"Name": "James",
"ID": "jamesJ",
"Sales": "5-B",
"FoodChoice": [
"Salsa",
"Salad"
]
}
]
}
这是我正在尝试使用的代码
var client = new RestClient("pretendAPI");
var request = new RestRequest(Method.GET);
request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
IRestResponse response = client.Execute(request);
var data = JsonConvert.DeserializeObject<JsonModel>(response.Content);
Console.WriteLine(data.details[0].Id); <--- NULL
这些是我试图将数据放入的对象
public class JsonModel
{
public List<Users> details { get; set; }
}
public class Users
{
public string Name;
public string ID;
public string Sales;
public List<string> FoodChoice;
}
解决方案
推荐阅读
- reactjs - 我可以在 React Hooks 中使用箭头函数而不是普通函数吗?
- excel - 没有标题的 Blue Prism Excel 导出
- fortran - Fortran 子模块共享相同的接口
- excel - 使用 Excel-VBA 在 Word 中填充表格并修改每个单元格中特定单词的颜色
- python - 使用 JSON 的 POST 在 Postman 中有效,但在 Python 中无效
- python - 正则表达式只将第一个匹配写入输出文件?
- python - 在远程 Web 服务器上使用 Django 构建 Web 应用程序的正确方法
- r - 是什么!!运算符/符号在 R 中代表什么?
- reactjs - React StepIconProps.classes 不适用
- java - 声纳问题:“覆盖此类中的“等于”方法