获取json invb.net

问题描述 投票:0回答:1

{ "Title": "The Capture", "Season": "1", "totalSeasons": "2", "Episodes": [ { "Title": "What Happens in Helmand", "Released": "2020-07-15", "Episode": "1", "imdbRating": "7.9", "imdbID": "tt8201448" }, { "Title": "Toy Soldier", "Released": "2020-07-15", "Episode": "2", "imdbRating": "8.2", "imdbID": "tt8201450" }, { "Title": "Truffle Hog", "Released": "2020-07-15", "Episode": "3", "imdbRating": "8.3", "imdbID": "tt8201452" }, { "Title": "Blind Spots", "Released": "2020-07-15", "Episode": "4", "imdbRating": "8.2", "imdbID": "tt8201454" }, { "Title": "A Pilgrim of Justice", "Released": "2020-07-15", "Episode": "5", "imdbRating": "8.3", "imdbID": "tt8201458" }, { "Title": "Correction", "Released": "2020-07-15", "Episode": "6", "imdbRating": "7.7", "imdbID": "tt8201460" }, { "Title": "Episode Seven", "Released": "2020-10-04", "Episode": "7", "imdbRating": "N/A", "imdbID": "tt15239694" }, { "Title": "Episode Eight", "Released": "2020-10-04", "Episode": "8", "imdbRating": "N/A", "imdbID": "tt15239700" } ], "Response": "True" }

这是上述代码的结果。
我如何获取要显示的情节列表??? 感谢帮助!

与情节班一起训练
Public Class Episode
Public Property Title As String
Public Property Released As String
Public Property Episode As String
Public Property imdbRating As String
Public Property imdbID As String
End Class

enter image description here ,所以它的工作方式?

在网络土地上,数据经常以jason的形式返回。 因此,您想使用所谓的避难所。

这样做的是将JSON数据取出,然后将字符串数据解析到对象中。在这种情况下,我们的vb.net对象。 wove,JSON不仅是一些行数据,而且具有一些属性,例如标题(这不是重复数据)。但是,有些数据正在重复。

因此,您需要创建一个具有单个值的对象模型,然后还具有esposides的“列表”。

,您需要这个:
Public Class MyShows
    Public Property Title As String
    Public Property Season As String
    Public Property totalSeasons As String
    Public Property Episodes As List(Of Episode)
    Public Property Response As String

End Class

Public Class Episode
    Public Property Title As String
    Public Property Released As String
    Public Property Episode As String
    Public Property imdbRating As String
    Public Property imdbID As String

End Class
json vb.net
1个回答
0
投票
then在vs中,使用这样的糊状特殊:

上面方便的提示可以节省您为JSON重新定位的类的手工编码。

否则,VS倾向于创建一个用于重复数据的“数组”。我发现10次中有9次不起作用,因此请更改:

Public Property Episodes() As Episode

to

Public Property Episodes As List(Of Episode)
我不喜欢默认的“ rootObject”名称。因此,由此产生的两个类的确与您所拥有的相同,因此:

Public Class MyShows Public Property Title As String Public Property Season As String Public Property totalSeasons As String Public Property Episodes As List(Of Episode) Public Property Response As String End Class Public Class Episode Public Property Title As String Public Property Released As String Public Property Episode As String Public Property imdbRating As String Public Property imdbID As String End Class

现在,我没有您的API密钥,但是您提供了JSON数据。因此,我刚刚将其粘贴到文本文件中,然后“读取”该文件代替您的Web服务调用。

您需要对该项目的这两个参考: enter image description here

或您可以考虑使用非常流行的纽顿索福。

,现在我们背后的代码是:
Imports System.IO
Imports System.Text.Json

Public Class ShowEpsodes
    Private Sub cmdGetShows_Click(sender As Object, e As EventArgs) Handles cmdGetShows.Click

        'Get the movie title to seach for
        Dim strTitle As String = SearchControlMovies.Text.Trim

        Dim sResult As String = File.ReadAllText("c:\test2\Shows.txt")

        Dim MyShows As clsMyShows = JsonSerializer.Deserialize(Of clsMyShows)(sResult)

        lblTitle.Text = MyShows.Title
        lblSeason.Text = MyShows.Season

        DataGridView1.DataSource = MyShows.Episodes


    End Sub

End Class

现在结果是:


    

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.