【发布时间】:2020-03-30 21:52:35
【问题描述】:
下面的代码没有返回任何错误,但仍然没有将 JSON 转换为对象
我从 API 获得的 JSON 字符串
{
"genres": [
{
"id": 28,
"name": "Action"
},
{
"id": 12,
"name": "Adventure"
}
]
}
一般测试类
public class Test
{
public int id;
public string Name;
}
下面的代码显示了我如何尝试将 JSON 字符串转换为测试类列表
string JsontStr = GenreService.get();
var Serializer = new JavaScriptSerializer();
List<Test> a = (List<Test>)Serializer.Deserialize(JsontStr, typeof(List<Test>));
an image of what the object a has in it when the program has finished running
【问题讨论】:
-
使用How to auto-generate a C# class file from a JSON string 生成正确的数据模型。您需要一个根对象
public class Welcome { public List<Genre> Genres { get; set; } },如app.quicktype.io?share=YdFGzyTvueCwJfGBb3Op 所示,这是链接问题的答案中提到的工具之一。
标签: c# asp.net api serialization jsonserializer