在列表视图中显示 JSON 响应 C# MVC

问题描述

我是 C# 新手,Android 是我的过去。如果我没有正确解释或理解,很抱歉。
试图弄清楚如何在视图中显示多个 JSON 响应。这是我使用 shtml 的类:

 public class Artist@R_392_4045@ion : 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 Artist@R_392_4045@ion(string artistName)
    {
        ArtistInfoResponse artistResponse = _musicRepository.GetArtistResponse(artistName);
        Artist@R_392_4045@ion viewmodel = new Artist@R_392_4045@ion();
        
        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>
}
,

我最终摆脱了类视图来填充 shtml,因为我想我不完全了解这个基础结构。我还删除了序列化对象本身的 IEnumerable 是 IEnumerable(我认为)。我现在有这样的 shtml:

@model MyMusicApp.Models.ArtistInfoResponse

@{ ViewData["Title"] = "艺术家信息"; }

<div class="container">

    <div class="form-group col-md-offset-3 col-md-5">

        <h2>Song Titles and Albums for @Model.Data[1].Artist.Name</h2>

        @foreach (var item in Model.Data)
        {
            <span class="badge">@item.Title</span>
            <br />

            <span class="badge">@item.Album.Title</span>
            <br />

            <img src="@Url.Content(item.Album.Cover)" />
            <hr />
        }

    </div>

</div>

<div class="container">

    <div class="form-group col-md-offset-3 col-md-5">

        <form method="get">
            <button asp-controller="Home" asp-action="SearchArtist" class="btn btn-success">Return</button>
        </form>

并填充它,将其从控制器传递到视图:

 public IActionResult ArtistInformation(string artistName)
    {
        ArtistInfoResponse artistResponse = _musicRepository.GetArtistResponse(artistName);
       
        return View(artistResponse);
    }

我的客户在一个单独的类中,其中包含 GetArtistResponse:

ArtistInfoResponse IMusicRepository.GetArtistResponse(string artistName)
    {
        artistName = (char)34 + artistName + (char)34;
        var client = new RestClient($"https://api.deezer.com/search?q=artist:{artistName}");

        var 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);

            // Deserialize the JToken object into our ArtistInfoResponse Class
            return content.ToObject<ArtistInfoResponse>();
        }

        return null;
    }
}

}