【问题标题】:Display JSON Response in List View C# MVC在列表视图 C# MVC 中显示 JSON 响应
【发布时间】:2021-05-31 14:34:58
【问题描述】:

我是 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; }
}

}

【问题讨论】:

  • 你能分享你的 ArtistInfoResponse 课程吗?

标签: c# json layout ienumerable


【解决方案1】:

你必须遍历你的数据,因为它是一个列表。

@foreach (var item in Model)
{
    <div>@item.Name</div>
    <div>@item.Album</div>
}

【讨论】:

    【解决方案2】:

    我最终摆脱了类视图来填充 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;
        }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-21
      • 2015-05-11
      • 1970-01-01
      • 1970-01-01
      • 2012-12-09
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多