c# - 将 JSON 中的数组转换为列表
问题描述
您好,这是我的 JSON 服务器:
http://api.tvmaze.com/search/shows?q=boys
我使用 REST API 根据我想从 JSON 获取的数据创建一个新列表,这是我创建的类:
public class TVMazeModel
{
public Show show { get; set; }
public Rating rating { get; set; }
public Image image { get; set; }
}
public class Show
{
public int id { get; set; }
public string url { get; set; }
public string name { get; set; }
}
public class Rating
{
public decimal average { get; set; }
}
public class Image
{
public string original { get; set; }
}
和 TVMazeController :
[HttpGet]
[Route("{searchText}")]
public IActionResult GetProductsExpensiveThan(string searchText)
{
try
{
var json = new WebClient().DownloadString("http://api.tvmaze.com/search/shows?q=" + searchText);
var objResponse1 = JsonConvert.DeserializeObject<List<TVMazeModel>>(json);
var json2 = new JavaScriptSerializer().Serialize(objResponse1);
return Ok(json2);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
}
当我运行服务器端并在 Internet 中浏览时:https://localhost:44395/api/TVMaze/boys
这就是我得到的:
com/shows/1522/mrs-browns-boys","name":"Mrs. 布朗男孩"},"rating":null,"image":null},{"show":{"id":1265,"url":"http://www.tvmaze.com/shows/1265/angry -boys","name":"愤怒的男孩"},"rating":null,"image":null}]
那么为什么 rating 是 null 而 image 是 null 呢?
节目的部分效果很好,只是他们有问题..
我想像我上面写的类一样获得 rating.average 和 image.original ...
解决方案
根据我从https://api.tvmaze.com/search/shows?q=a得到的回复,我意识到 Image 和 Rating 落后于 show,因此您必须创建与第三个结构相同的 TVMazeModel网址响应。
尝试这个:
public class TVMazeModel
{
public Show show { get; set; }
}
public class Show
{
public int id { get; set; }
public string url { get; set; }
public string name { get; set; }
public Rating rating { get; set; }
public Image image { get; set; }
}
public class Rating
{
public decimal average { get; set; }
}
public class Image
{
public string original { get; set; }
}
推荐阅读
- python - 通过 python 将数据记录器中的实时数据保存到 csv 文件
- php - 从会话购物车中读取多个项目时出现问题
- javascript - 使用 jQuery-EasyFilter 过滤“多类别”
- json - VBA-JSON parser v2.2.3 运行时不一致
- java - 是否可以将数字分配给字符串,然后将这些数字放入出队系统中?
- javascript - 如何显示/隐藏什么时候
被选中/未选中? - c# - 使用模板和创建冲突来创建人员对象
- excel - 如何在word文档中表格的同一单元格中添加2张来自excel的jpeg图片?
- cassandra - 无法使用 brew 在本地运行 Cassandra
- vba - 根据匹配结果从 wkbk 1 返回值到 wkbk 2