You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET调用OMDB API解析JSON无剧集数据问题求助

VB.NET调用OMDB API获取剧集列表无数据显示问题解决

问题概述

使用VB.NET调用OMDB API获取电视剧剧集列表时,能正常获取到总集数,但无法显示具体的剧集数据。

原实现代码

Imports System.Net
Imports Newtonsoft.Json

Public Class MainForm
    Private Sub btnGetEpisodes_Click(sender As Object, e As EventArgs) Handles btnGetEpisodes.Click
        Dim apiKey As String = "你的API密钥"
        Dim seriesTitle As String = "Breaking Bad"
        Dim url As String = $"http://www.omdbapi.com/?t={seriesTitle}&type=series&apikey={apiKey}&Season=1"

        Using client As New WebClient()
            Dim jsonResponse As String = client.DownloadString(url)
            Dim result As EpisodeResult = JsonConvert.DeserializeObject(Of EpisodeResult)(jsonResponse)
            
            MessageBox.Show($"总集数:{result.totalSeasons}")
            ' 遍历剧集无数据显示
            For Each ep In result.Episodes
                lstEpisodes.Items.Add($"S{ep.Season}E{ep.Episode}:{ep.Title}")
            Next
        End Using
    End Sub
End Class

OMDB API返回的JSON结构

{
  "Title": "Breaking Bad",
  "Season": "1",
  "totalSeasons": "5",
  "Episodes": [
    {
      "Title": "Pilot",
      "Episode": "1",
      "Season": "1",
      "Released": "2008-01-20",
      "imdbRating": "9.0",
      "imdbID": "tt0903747"
    },
    {
      "Title": "Cat's in the Bag...",
      "Episode": "2",
      "Season": "1",
      "Released": "2008-01-27",
      "imdbRating": "8.7",
      "imdbID": "tt0903748"
    }
  ],
  "Response": "True"
}

原自定义Episode类

Public Class EpisodeResult
    Public Property totalSeasons As String
    Public Property Episodes As List(Of Episode)
End Class

Public Class Episode
    Public Property Title As String
    Public Property Season As String
    Public Property Episode As String
End Class

问题原因

  1. URL未编码:剧集标题包含空格等特殊字符时,直接拼接URL会导致API请求解析异常,返回的JSON可能缺失剧集数据。
  2. 编码未指定:WebClient默认编码可能与API返回的UTF-8编码不匹配,导致JSON解析错误。
  3. 实体类映射不明确:未显式指定JSON键与类属性的映射关系,存在潜在的名称匹配问题。
  4. 未校验API响应状态:无法及时发现请求失败的情况,误导为无数据显示。

修正后可正常运行的代码

Imports System.Net
Imports Newtonsoft.Json

Public Class MainForm
    Private Sub btnGetEpisodes_Click(sender As Object, e As EventArgs) Handles btnGetEpisodes.Click
        Dim apiKey As String = "你的API密钥"
        Dim seriesTitle As String = "Breaking Bad"
        ' 对标题进行URL编码,处理特殊字符
        Dim encodedTitle As String = Uri.EscapeDataString(seriesTitle)
        Dim url As String = $"http://www.omdbapi.com/?t={encodedTitle}&type=series&apikey={apiKey}&Season=1"

        Using client As New WebClient()
            ' 指定UTF-8编码,确保正确解析JSON
            client.Encoding = System.Text.Encoding.UTF8
            Dim jsonResponse As String = client.DownloadString(url)
            
            ' 先校验API响应状态
            Dim rawResult As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(jsonResponse)
            If rawResult("Response").ToString() = "True" Then
                Dim result As EpisodeResult = JsonConvert.DeserializeObject(Of EpisodeResult)(jsonResponse)
                
                MessageBox.Show($"总集数:{result.totalSeasons}")
                lstEpisodes.Items.Clear()
                For Each ep In result.Episodes
                    lstEpisodes.Items.Add($"S{ep.SeasonNumber}E{ep.EpisodeNumber}:{ep.Title}")
                Next
            Else
                MessageBox.Show($"API请求失败:{rawResult("Error").ToString()}")
            End If
        End Using
    End Sub
End Class

' 显式映射JSON键与类属性,避免匹配问题
Public Class EpisodeResult
    Public Property Title As String
    Public Property Season As String
    Public Property totalSeasons As String
    <JsonProperty("Episodes")>
    Public Property Episodes As List(Of Episode)
    Public Property Response As String
    Public Property Error As String
End Class

Public Class Episode
    <JsonProperty("Title")>
    Public Property Title As String
    <JsonProperty("Episode")>
    Public Property EpisodeNumber As String
    <JsonProperty("Season")>
    Public Property SeasonNumber As String
    <JsonProperty("Released")>
    Public Property ReleasedDate As String
    <JsonProperty("imdbRating")>
    Public Property ImdbRating As String
End Class

内容的提问来源于stack exchange,提问作者Robert

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 14:05:18