c# - 在列表视图 C# MVC 中显示 JSON 响应
问题描述
我是 C# 新手,Android 是我的过去。很抱歉,如果我没有正确解释或理解。
试图弄清楚如何在视图中显示多个我的 JSON 响应。这是我要使用 shtml 的课程:
public class ArtistInformation : IEnumerable
{
[Display(Name = "Song Title:")]
public string Name { get; set; }
[Display(Name = "Album Title:")]
public string Album { get; set; }
[DataType(DataType.Url)]
public string Image { get; set; }
}
shtml:
<div class="form-group col-md-offset-3 col-md-5">
<h2>Forecast for the selected city</h2>
<label asp-for="Name"></label>
<span class="badge">@Model.Name</span>
<br />
<label asp-for="Album"></label>
<span class="badge">@Model.Album</span>
<br />
<label asp-for="Image"></label>
<div class="display-field">
<img src="@Url.Content(Model.Image)/>"
<br />
</div>
这是我的控制器,我在其中硬编码了要显示的第一个位置,以确保它有效:
public IActionResult ArtistInformation(string artistName)
{
ArtistInfoResponse artistResponse = _musicRepository.GetArtistResponse(artistName);
ArtistInformation viewModel = new ArtistInformation();
if (artistResponse != null)
{
viewModel.Name = artistResponse.Data[1].Title;
viewModel.Album = artistResponse.Data[1].Album.Title;
viewModel.Image = artistResponse.Data[1].Album.Cover;
}
return View(viewModel);
}
现在我想用我的整个回答重复这个观点。我已经阅读了 IEnumerable 但如果这是我需要的,我不知道如何申请。
这是我使用 RestSharp 获取 JSON 响应的地方:
ArtistInfoResponse IMusicRepository.GetArtistResponse(string artistName)
{
artistName = (char)34 + artistName + (char)34; //ensure the string is wa
RestClient client = new RestClient($"https://api.deezer.com/search?q=artist:{artistName}");
RestRequest request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
if (response.IsSuccessful)
{
// Deserialize the string content into JToken object
// var content = JsonConvert.DeserializeObject<JToken>(response.Content);
var content = JsonConvert.DeserializeObject<IEnumerable<ArtistInfoResponse>>(response.Content);
// Deserialize the JToken object into our ArtistInfoResponse Class
return content.ToObject<ArtistInfoResponse>(); // this line doesn't work now
}
return null;
}
}
编辑添加类******************************
public class ArtistInfoResponse : IEnumerable
{
public SongInfo[] Data { get; set; }
public int Total { get; set; }
public string Next { get; set; }
public IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
}
public class SongInfo
{
public int Id { get; set; }
public bool Readable { get; set; }
public string Title { get; set; }
public string Title_short { get; set; }
public string Title_version { get; set; }
public string Link { get; set; }
public int Duration { get; set; }
public int Rank { get; set; }
public bool Explicit_lyrics { get; set; }
public int Explicit_content_lyrics { get; set; }
public int Explicit_content_cover { get; set; }
public string Preview { get; set; }
public string Md5_image { get; set; }
public Artist Artist { get; set; }
public Album Album { get; set; }
public string Type { get; set; }
}
public class Artist
{
public int Id { get; set; }
public string Name { get; set; }
public string Link { get; set; }
public string Picture { get; set; }
public string Picture_small { get; set; }
public string Picture_medium { get; set; }
public string Picture_big { get; set; }
public string Picture_xl { get; set; }
public string Tracklist { get; set; }
public string Type { get; set; }
}
public class Album
{
public int Id { get; set; }
public string Title { get; set; }
public string Cover { get; set; }
public string Cover_small { get; set; }
public string Cover_medium { get; set; }
public string Cover_big { get; set; }
public string Cover_xl { get; set; }
public string Md5_image { get; set; }
public string Tracklist { get; set; }
public string Type { get; set; }
}
}
解决方案
您必须遍历您的数据,因为它是一个列表。
@foreach (var item in Model)
{
<div>@item.Name</div>
<div>@item.Album</div>
}
推荐阅读
- bash - wg-quick "wait: pid 9185 is not a child if this shell" on Debian 8.11
- python - 删除 Matplotlib,Python 中某些子图的 y 轴刻度标签
- r - 如何在 R XGboost 中找到每个预测的重要变量
- python - 计算一组因变量的滚动窗口多元线性回归的有效方法?
- javascript - JavaScript 中的位运算符是否有安全的替代方案?
- php - 如何使用简码显示 Woo Commerce 评论?
- sql - 将项目加在一起但单独计算,组和子组?
- javascript - 动态创建的 DOM 未显示在组件中
- git - 子分支的 Git 拉取请求
- javascript - jquery 数据表:“未捕获的类型错误:无法读取未定义的属性‘长度’”在销毁和重新初始化后。表作为数据源