I am trying to get the list of episodes for tv series. here is the code in vb that i am using. it shows correct amount of return, but no data.
Private Sub FindEpisodes()
Try
'Get the movie title to seach for
Dim strTitle As String = SearchControlMovies.Text.Trim
Dim strSeasons As String = "/?i=" & strTitle & "Season=1" & "&apikey=" & "****"
'search for TV Series Episodes
Dim Req As HttpWebRequest
Dim Ret As HttpWebResponse = Nothing
Dim SR As StreamReader
Req = DirectCast(WebRequest.Create(strSeasons), HttpWebRequest)
Ret = DirectCast(Req.GetResponse(), HttpWebResponse)
SR = New StreamReader(Ret.GetResponseStream())
Dim Raw As String = Nothing
Raw = SR.ReadToEnd()
Dim JavaScriptSerialization As New JavaScriptSerializer()
Dim episodelist As New Episode()
episodelist = CType(JavaScriptSerialization.Deserialize(Raw, episodelist.GetType), Episode)
'show title and season for selection
LblSeriesEpisodes.Text = "Title: " & TxtTVSeriesTitle.Text & vbCrLf & "Season: 1" & vbCrLf & "Total Seasons: " & TxtTVSeriesSeasons.Text & vbCrLf & vbCrLf & "Episodes" & vbCrLf & vbCrLf
Dim i As Integer
For i = 0 To Raw.Count - 1
LblSeriesEpisodes.Text = LblSeriesEpisodes.Text & "Title: " & episodelist.Title & vbCrLf & "Released: " & episodelist.Released & vbCrLf & "Epsode: " & episodelist.Episode & vbCrLf & "Rating: " & episodelist.imdbRating & vbCrLf & "ID: " & episodelist.imdbID & vbCrLf & vbCrLf
Next
Catch ex As Exception
XtraMessageBox.Show(ex.Message, Application.ProductName & " - TVSeriesSearchControl.Validating Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Err.Clear()
End Try
End Sub
Here is the JSon returned
{
"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"
}
this is the result of above code.
as you can see, no data appears?
How can i get the list of episodes to show??? Appreciate the help!
Update with the Episode 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
Upadted answer from MS Copilot. works perfectly.
Imports System.Net.Http
Imports Newtonsoft.Json
Public Class Show
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
Public Class MainForm
Inherits Form
Private ReadOnly client As New HttpClient()
Private dataGridView As New DataGridView()
Private Async Function GetJsonResponseAsync(url As String) As Task(Of String)
Dim response As HttpResponseMessage = Await client.GetAsync(url)
response.EnsureSuccessStatusCode()
Return Await response.Content.ReadAsStringAsync()
End Function
Public Sub New()
' Initialize the form and DataGridView
Me.Text = "JSON Data in GridView"
Me.Size = New Size(800, 600)
dataGridView.Dock = DockStyle.Fill
Me.Controls.Add(dataGridView)
' Load the JSON data and bind it to the DataGridView
LoadAndBindData()
End Sub
Private Async Sub LoadAndBindData()
Dim strTitle As String = "tt4154796" ' Example IMDb ID
Dim strapikey As String = "your_api_key" ' Your OMDB API key
Dim strSeasons As String = "/?i=" & strTitle & "&Season=1" & "&apikey=" & strapikey
Dim jsonResponse As String = Await GetJsonResponseAsync(strSeasons)
' Parse the JSON response
Dim show As Show = JsonConvert.DeserializeObject(Of Show)(jsonResponse)
' Bind the Episodes list to the DataGridView
dataGridView.DataSource = show.Episodes
End Sub
End Class
I am trying to get the list of episodes for tv series. here is the code in vb that i am using. it shows correct amount of return, but no data.
Private Sub FindEpisodes()
Try
'Get the movie title to seach for
Dim strTitle As String = SearchControlMovies.Text.Trim
Dim strSeasons As String = "https://www.omdbapi/?i=" & strTitle & "Season=1" & "&apikey=" & "****"
'search for TV Series Episodes
Dim Req As HttpWebRequest
Dim Ret As HttpWebResponse = Nothing
Dim SR As StreamReader
Req = DirectCast(WebRequest.Create(strSeasons), HttpWebRequest)
Ret = DirectCast(Req.GetResponse(), HttpWebResponse)
SR = New StreamReader(Ret.GetResponseStream())
Dim Raw As String = Nothing
Raw = SR.ReadToEnd()
Dim JavaScriptSerialization As New JavaScriptSerializer()
Dim episodelist As New Episode()
episodelist = CType(JavaScriptSerialization.Deserialize(Raw, episodelist.GetType), Episode)
'show title and season for selection
LblSeriesEpisodes.Text = "Title: " & TxtTVSeriesTitle.Text & vbCrLf & "Season: 1" & vbCrLf & "Total Seasons: " & TxtTVSeriesSeasons.Text & vbCrLf & vbCrLf & "Episodes" & vbCrLf & vbCrLf
Dim i As Integer
For i = 0 To Raw.Count - 1
LblSeriesEpisodes.Text = LblSeriesEpisodes.Text & "Title: " & episodelist.Title & vbCrLf & "Released: " & episodelist.Released & vbCrLf & "Epsode: " & episodelist.Episode & vbCrLf & "Rating: " & episodelist.imdbRating & vbCrLf & "ID: " & episodelist.imdbID & vbCrLf & vbCrLf
Next
Catch ex As Exception
XtraMessageBox.Show(ex.Message, Application.ProductName & " - TVSeriesSearchControl.Validating Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Err.Clear()
End Try
End Sub
Here is the JSon returned
{
"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"
}
this is the result of above code.
as you can see, no data appears?
How can i get the list of episodes to show??? Appreciate the help!
Update with the Episode 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
Upadted answer from MS Copilot. works perfectly.
Imports System.Net.Http
Imports Newtonsoft.Json
Public Class Show
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
Public Class MainForm
Inherits Form
Private ReadOnly client As New HttpClient()
Private dataGridView As New DataGridView()
Private Async Function GetJsonResponseAsync(url As String) As Task(Of String)
Dim response As HttpResponseMessage = Await client.GetAsync(url)
response.EnsureSuccessStatusCode()
Return Await response.Content.ReadAsStringAsync()
End Function
Public Sub New()
' Initialize the form and DataGridView
Me.Text = "JSON Data in GridView"
Me.Size = New Size(800, 600)
dataGridView.Dock = DockStyle.Fill
Me.Controls.Add(dataGridView)
' Load the JSON data and bind it to the DataGridView
LoadAndBindData()
End Sub
Private Async Sub LoadAndBindData()
Dim strTitle As String = "tt4154796" ' Example IMDb ID
Dim strapikey As String = "your_api_key" ' Your OMDB API key
Dim strSeasons As String = "https://www.omdbapi/?i=" & strTitle & "&Season=1" & "&apikey=" & strapikey
Dim jsonResponse As String = Await GetJsonResponseAsync(strSeasons)
' Parse the JSON response
Dim show As Show = JsonConvert.DeserializeObject(Of Show)(jsonResponse)
' Bind the Episodes list to the DataGridView
dataGridView.DataSource = show.Episodes
End Sub
End Class
Share
Improve this question
edited Feb 2 at 20:51
Robert
asked Feb 1 at 19:17
RobertRobert
457 bronze badges
3
- The following Nuget packages may be of interest: Newtonsoft.Json and System.Text.Json. For .NET Framework use Newtonsoft.Json. If you're using .NET, then use System.Text.Json. – It all makes cents Commented Feb 1 at 19:29
- The following may also be of interest: JSON Utils. – It all makes cents Commented Feb 1 at 19:30
- fot to post the classes. already have them in code. – Robert Commented Feb 1 at 21:05
1 Answer
Reset to default 1Ok, so the way this works?
In web land, data is "very" often returned as JSON.
Hence, you want to use what is called deserialization.
What that does is takes the JSON data, and parses out the string data into a object. In this case, our vb object.
However, that JSON is not only JUST some rows of repeating data, but has some attributes such as Title (which is not repeating data). But, some of the data is repeating.
So, you need to create a object model that has both the single values, and then also has the "list" of episodes (the repeating data list).
Hence, you need this:
Public Class clsMyShows
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
And keep in mind, that you can (often) take (cut/copy) the JSON text, and then use in VS edit-Paste special to auto generate this class for you.
So, say you put the JSON data in notepad, and select all the text (and ctrl-c to copy)
Then in VS, use paste special like this:
The above handy tip can save you the hand coding of the class required for the JSON.
However, VS tends to create an "array" for repeating data. I find 9 out of 10 times the array does not work, so any array() in the class, change over to a list of object type.
Hence, change this array to a list:
eg:
Public Property Episodes() As Episode
to
Public Property Episodes As List(Of Episode)
So, just keep in mind that if you use the paste special feature, in near all cases you have to tweak the code somewhat, and change arrays to list of "some class".
And I did not like the default "rootobject" name from the paste, so I renamed it as clsMyShows.
So, the resulting main class is required due to the "repeating" data, and the non repeating data. Note how the 2nd class is the same as what you had created.
So, now we have this:
Public Class clsMyShows
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
Now, I don't have your api key, but you provided your JSON data. So, I just pasted it into a text file, and "read" that in place of your web service call.
And you need these two references to the project:
Or you could consider using the very popular Newton Soft library. (install with Nugget)
So, now our code behind is thus this:
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 search 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
And the result is now this:
So, to use some "de serialization" library, you need to create a valid object that "allows" the data to flow into that object. Hence, we in effect wind up with 2 class's to deal with such data.