0

我试图让用户在我的程序中输入电影标题,然后让程序返回烂番茄数据库中匹配的前三部电影。

这是我到目前为止的代码:

Private Sub Query(ByVal searchTerm As String)
        'declare the url, request, response and reader,
        Dim url As String
        Dim request As HttpWebRequest
        Dim response As HttpWebResponse = Nothing
        Dim reader As StreamReader

        Console.BackgroundColor = ConsoleColor.Red : Console.WriteLine(" QUERYING ROTTEN TOMATOES FOR THE SEARCHTERM {0} ", searchTerm) : Console.BackgroundColor = ConsoleColor.Black
        Try
            'base url, query, apikey, page (or result) limit,
            url = String.Format("http://api.rottentomatoes.com/api/public/v1.0/movies.json?q={0}&apikey={1}&page_limit={2}", searchTerm, myApiKey, "3")

            'make the request
            request = DirectCast(WebRequest.Create(url), HttpWebRequest)
            'get the response
            response = DirectCast(request.GetResponse(), HttpWebResponse)
            'read the response
            reader = New StreamReader(response.GetResponseStream())

            'declare a string for the raw response
            Dim rawResponse As String
            'get the response a raw string
            rawResponse = reader.ReadToEnd()
            'write the raw response to the console
            Console.WriteLine(rawResponse)

            'write that the rest of the data is specific
            Console.BackgroundColor = ConsoleColor.Red : Console.WriteLine(" EXTRACTED DATA: ") : Console.BackgroundColor = ConsoleColor.Black

            'parse the results from the rawresponse
            Dim jResults As JObject = JObject.Parse(rawResponse)

            Console.WriteLine(ParseTitle(jResults("total")))
            Console.WriteLine(ParseTitle(jResults("title")))
            'Console.WriteLine(ParseTitle(jResults("critics_score")))
        Catch ex As Exception
            Console.WriteLine("Error: " & ex.ToString)
        Finally
            If Not response Is Nothing Then response.Close()
        End Try

        Console.BackgroundColor = ConsoleColor.Red : Console.WriteLine(" QUERY FINISHED. PRESS ANY KEY TO CLOSE... ") : Console.BackgroundColor = ConsoleColor.Black
        Console.ReadKey()
    End Sub

我正在使用 JSON.Net 库,而且说实话,我没有使用 JSON 的经验——我一直在学习非教程。

我希望能够将所有电影返回到一系列电影数据结构中有人可以帮助我吗?

这是我搜索“玩具总动员”时得到的回复:

{
    "total": 7,
    "movies": [
        {
            "id": "770672122",
            "title": "Toy Story 3",
            "year": 2010,
            "mpaa_rating": "G",
            "runtime": 103,
            "critics_consensus": "Deftlyblending comedy, adventure, and honest emotion, Toy Story 3 is a raresecond sequel that reallyworks.",
            "release_dates": {
                "theater": "2010-06-18",
                "dvd": "2010-11-02"
            },
            "ratings": {
                "critics_rating": "CertifiedFresh",
                "critics_score": 99,
                "audience_rating": "Upright",
                "audience_score": 91
            },
            "synopsis": "Pixarreturns to their first success with Toy Story 3. The movie begins withAndy leaving for college and donating his beloved toys -- includingWoody (Tom Hanks) and Buzz (Tim Allen) -- to a daycare. While the crewmeets new friends, including Ken (Michael Keaton), they soon grow tohate their new surroundings and plan an escape. The film was directedby Lee Unkrich from a script co-authored by Little Miss Sunshinescribe Michael Arndt. ~ Perry Seibert,Rovi",
            "posters": {
                "thumbnail": "http://content6.flixster.com/movie/11/13/43/11134356_mob.jpg",
                "profile": "http://content6.flixster.com/movie/11/13/43/11134356_pro.jpg",
                "detailed": "http://content6.flixster.com/movie/11/13/43/11134356_det.jpg",
                "original": "http://content6.flixster.com/movie/11/13/43/11134356_ori.jpg"
            },
            "abridged_cast": [
                {
                    "name": "TomHanks",
                    "id": "162655641",
                    "characters": [
                        "Woody"
                    ]
                },
                {
                    "name": "TimAllen",
                    "id": "162655909",
                    "characters": [
                        "Buzz Lightyear"
                    ]
                },
                {
                    "name": "JoanCusack",
                    "id": "162655020",
                    "characters": [
                        "Jessie theCowgirl"
                    ]
                },
                {
                    "name": "NedBeatty",
                    "id": "162672460",
                    "characters": [
                        "Lots-o'-Huggin'Bear",
                        "Lotso"
                    ]
                },
                {
                    "name": "DonRickles",
                    "id": "341817905",
                    "characters": [
                        "Mr. PotatoHead"
                    ]
                }
            ],
            "alternate_ids": {
                "imdb": "0435761"
            },
            "links": {
                "self": "http://api.rottentomatoes.com/api/public/v1.0/movies/770672122.json",
                "alternate": "http://www.rottentomatoes.com/m/toy_story_3/",
                "cast": "http://api.rottentomatoes.com/api/public/v1.0/movies/770672122/cast.json",
                "clips": "http://api.rottentomatoes.com/api/public/v1.0/movies/770672122/clips.json",
                "reviews": "http://api.rottentomatoes.com/api/public/v1.0/movies/770672122/reviews.json",
                "similar": "http://api.rottentomatoes.com/api/public/v1.0/movies/770672122/similar.json"
            }
        },
        {
            "id": "9414",
            "title": "ToyStory2",
            "year": 1999,
            "mpaa_rating": "G",
            "runtime": 92,
            "critics_consensus": "ToyStory 2 employs inventive storytelling, gorgeous animation, and a topnotch voice cast to deliver another rich moviegoing experience for allages, one that's arguably even better than itspredecessor.",
            "release_dates": {
                "theater": "1999-11-24",
                "dvd": "2000-10-17"
            },
            "ratings": {
                "critics_rating": "CertifiedFresh",
                "critics_score": 100,
                "audience_rating": "Upright",
                "audience_score": 72
            },
            "synopsis": "",
            "posters": {
                "thumbnail": "http://content6.flixster.com/movie/10/93/63/10936392_mob.jpg",
                "profile": "http://content6.flixster.com/movie/10/93/63/10936392_pro.jpg",
                "detailed": "http://content6.flixster.com/movie/10/93/63/10936392_det.jpg",
                "original": "http://content6.flixster.com/movie/10/93/63/10936392_ori.jpg"
            },
            "abridged_cast": [
                {
                    "name": "TomHanks",
                    "id": "162655641",
                    "characters": [
                        "Woody"
                    ]
                },
                {
                    "name": "TimAllen",
                    "id": "162655909",
                    "characters": [
                        "Buzz Lightyear"
                    ]
                },
                {
                    "name": "JoanCusack",
                    "id": "162655020",
                    "characters": [
                        "Jessie theCowgirl"
                    ]
                },
                {
                    "name": "KelseyGrammer",
                    "id": "162660300",
                    "characters": [
                        "Stinky Pete theProspector"
                    ]
                },
                {
                    "name": "DonRickles",
                    "id": "341817905",
                    "characters": [
                        "Mr. PotatoHead"
                    ]
                }
            ],
            "alternate_ids": {
                "imdb": "0120363"
            },
            "links": {
                "self": "http://api.rottentomatoes.com/api/public/v1.0/movies/9414.json",
                "alternate": "http://www.rottentomatoes.com/m/toy_story_2/",
                "cast": "http://api.rottentomatoes.com/api/public/v1.0/movies/9414/cast.json",
                "clips": "http://api.rottentomatoes.com/api/public/v1.0/movies/9414/clips.json",
                "reviews": "http://api.rottentomatoes.com/api/public/v1.0/movies/9414/reviews.json",
                "similar": "http://api.rottentomatoes.com/api/public/v1.0/movies/9414/similar.json"
            }
        },
        {
            "id": "9559",
            "title": "ToyStory",
            "year": 1995,
            "mpaa_rating": "G",
            "runtime": 80,
            "critics_consensus": "Asentertaining as it is innovative, Toy Story kicked off Pixar'sunprecedented run of quality pictures, reinvigorating animated film intheprocess.",
            "release_dates": {
                "theater": "1995-11-22",
                "dvd": "2001-03-20"
            },
            "ratings": {
                "critics_rating": "CertifiedFresh",
                "critics_score": 100,
                "audience_rating": "Upright",
                "audience_score": 81
            },
            "synopsis": "",
            "posters": {
                "thumbnail": "http://content7.flixster.com/movie/10/93/63/10936393_mob.jpg",
                "profile": "http://content7.flixster.com/movie/10/93/63/10936393_pro.jpg",
                "detailed": "http://content7.flixster.com/movie/10/93/63/10936393_det.jpg",
                "original": "http://content7.flixster.com/movie/10/93/63/10936393_ori.jpg"
            },
            "abridged_cast": [
                {
                    "name": "TomHanks",
                    "id": "162655641",
                    "characters": [
                        "Woody"
                    ]
                },
                {
                    "name": "TimAllen",
                    "id": "162655909",
                    "characters": [
                        "Buzz Lightyear"
                    ]
                },
                {
                    "name": "DonRickles",
                    "id": "341817905",
                    "characters": [
                        "Mr. PotatoHead"
                    ]
                },
                {
                    "name": "Jim Varney",
                    "id": "162662792",
                    "characters": [
                        "SlinkyDog"
                    ]
                },
                {
                    "name": "WallaceShawn",
                    "id": "162671862",
                    "characters": [
                        "Rex"
                    ]
                }
            ],
            "alternate_ids": {
                "imdb": "0114709"
            },
            "links": {
                "self": "http://api.rottentomatoes.com/api/public/v1.0/movies/9559.json",
                "alternate": "http://www.rottentomatoes.com/m/toy_story/",
                "cast": "http://api.rottentomatoes.com/api/public/v1.0/movies/9559/cast.json",
                "clips": "http://api.rottentomatoes.com/api/public/v1.0/movies/9559/clips.json",
                "reviews": "http://api.rottentomatoes.com/api/public/v1.0/movies/9559/reviews.json",
                "similar": "http://api.rottentomatoes.com/api/public/v1.0/movies/9559/similar.json"
            }
        }
    ],
    "links": {
        "self": "http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=toy+story&page_limit=3&page=1",
        "next": "http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=toy+story&page_limit=3&page=2"
    },
    "link_template": "http://api.rottentomatoes.com/api/public/v1.0/movies.json?q={search-term}&page_limit={results-per-page}&page={page-number}"
}
4

1 回答 1

1

首先,您需要创建类来接收 JSON 数据:

Public Class ReleaseDates

    Public Property theater As String
    Public Property dvd As String

End Class

Public Class Ratings

    Public Property critics_rating As String
    Public Property critics_score As Integer
    Public Property audience_rating As String
    Public Property audience_score As Integer

End Class

Public Class Posters

    Public Property thumbnail As String
    Public Property profile As String
    Public Property detailed As String
    Public Property original As String

End Class

Public Class AbridgedCast

    Public Property name As String
    Public Property id As String
    Public Property characters As List(Of String)

End Class

Public Class AlternateIds

    Public Property imdb As String

End Class

Public Class Links

    Public Property self As String
    Public Property alternate As String
    Public Property cast As String
    Public Property clips As String
    Public Property reviews As String
    Public Property similar As String

End Class

Public Class Movie

    Public Property id As String
    Public Property title As String
    Public Property year As Integer
    Public Property mpaa_rating As String
    Public Property runtime As Integer
    Public Property critics_consensus As String
    Public Property release_dates As ReleaseDates
    Public Property ratings As Ratings
    Public Property synopsis As String
    Public Property posters As Posters
    Public Property abridged_cast As List(Of AbridgedCast)
    Public Property alternate_ids As AlternateIds
    Public Property links As Links

End Class

Public Class Links2

    Public Property self As String
    <JsonProperty(PropertyName:="next")>
    Public Property nextItem As String

End Class

Public Class RootObject

    Public Property total As Integer
    Public Property movies As List(Of Movie)
    Public Property links As Links2
    Public Property link_template As String

End Class

完成后,您可以简单地调用JsonConvert.DeserializeObject以反序列化您的 JSON 数据:

Sub Main()

    Dim json As String

    json = "{""total"":7,""movies"":[{""id"":""770672122"",""title"":""Toy Story 3"",""year"":2010,""mpaa_rating"":""G"",""runtime"":103,""critics_consensus"":""Deftly blending comedy, adventure, and honest emotion, Toy Story 3 is a rare second sequel that really works."",""release_dates"":{""theater"":""2010-06-18"",""dvd"":""2010-11-02""},""ratings"":{""critics_rating"":""Certified Fresh"",""critics_score"":99,""audience_rating"":""Upright"",""audience_score"":91},""synopsis"":""Pixar returns to their first success with Toy Story 3. The movie begins with Andy leaving for college and donating his beloved toys -- including Woody (Tom Hanks) and Buzz (Tim Allen) -- to a daycare. While the crew meets new friends, including Ken (Michael Keaton), they soon grow to hate their new surroundings and plan an escape. The film was directed by Lee Unkrich from a script co-authored by Little Miss Sunshine scribe Michael Arndt. ~ Perry Seibert, Rovi"",""posters"":{""thumbnail"":""http://content6.flixster.com/movie/11/13/43/11134356_mob.jpg"",""profile"":""http://content6.flixster.com/movie/11/13/43/11134356_pro.jpg"",""detailed"":""http://content6.flixster.com/movie/11/13/43/11134356_det.jpg"",""original"":""http://content6.flixster.com/movie/11/13/43/11134356_ori.jpg""},""abridged_cast"":[{""name"":""Tom Hanks"",""id"":""162655641"",""characters"":[""Woody""]},{""name"":""Tim Allen"",""id"":""162655909"",""characters"":[""Buzz Lightyear""]},{""name"":""Joan Cusack"",""id"":""162655020"",""characters"":[""Jessie the Cowgirl""]},{""name"":""Ned Beatty"",""id"":""162672460"",""characters"":[""Lots-o'-Huggin' Bear"",""Lotso""]},{""name"":""Don Rickles"",""id"":""341817905"",""characters"":[""Mr. Potato Head""]}],""alternate_ids"":{""imdb"":""0435761""},""links"":{""self"":""http://api.rottentomatoes.com/api/public/v1.0/movies/770672122.json"",""alternate"":""http://www.rottentomatoes.com/m/toy_story_3/"",""cast"":""http://api.rottentomatoes.com/api/public/v1.0/movies/770672122/cast.json"",""clips"":""http://api.rottentomatoes.com/api/public/v1.0/movies/770672122/clips.json"",""reviews"":""http://api.rottentomatoes.com/api/public/v1.0/movies/770672122/reviews.json"",""similar"":""http://api.rottentomatoes.com/api/public/v1.0/movies/770672122/similar.json""}},{""id"":""9414"",""title"":""Toy Story 2"",""year"":1999,""mpaa_rating"":""G"",""runtime"":92,""critics_consensus"":""Toy Story 2 employs inventive storytelling, gorgeous animation, and a top notch voice cast to deliver another rich moviegoing experience for all ages, one that's arguably even better than its predecessor."",""release_dates"":{""theater"":""1999-11-24"",""dvd"":""2000-10-17""},""ratings"":{""critics_rating"":""Certified Fresh"",""critics_score"":100,""audience_rating"":""Upright"",""audience_score"":72},""synopsis"":"""",""posters"":{""thumbnail"":""http://content6.flixster.com/movie/10/93/63/10936392_mob.jpg"",""profile"":""http://content6.flixster.com/movie/10/93/63/10936392_pro.jpg"",""detailed"":""http://content6.flixster.com/movie/10/93/63/10936392_det.jpg"",""original"":""http://content6.flixster.com/movie/10/93/63/10936392_ori.jpg""},""abridged_cast"":[{""name"":""Tom Hanks"",""id"":""162655641"",""characters"":[""Woody""]},{""name"":""Tim Allen"",""id"":""162655909"",""characters"":[""Buzz Lightyear""]},{""name"":""Joan Cusack"",""id"":""162655020"",""characters"":[""Jessie the Cowgirl""]},{""name"":""Kelsey Grammer"",""id"":""162660300"",""characters"":[""Stinky Pete the Prospector""]},{""name"":""Don Rickles"",""id"":""341817905"",""characters"":[""Mr. Potato Head""]}],""alternate_ids"":{""imdb"":""0120363""},""links"":{""self"":""http://api.rottentomatoes.com/api/public/v1.0/movies/9414.json"",""alternate"":""http://www.rottentomatoes.com/m/toy_story_2/"",""cast"":""http://api.rottentomatoes.com/api/public/v1.0/movies/9414/cast.json"",""clips"":""http://api.rottentomatoes.com/api/public/v1.0/movies/9414/clips.json"",""reviews"":""http://api.rottentomatoes.com/api/public/v1.0/movies/9414/reviews.json"",""similar"":""http://api.rottentomatoes.com/api/public/v1.0/movies/9414/similar.json""}},{""id"":""9559"",""title"":""Toy Story"",""year"":1995,""mpaa_rating"":""G"",""runtime"":80,""critics_consensus"":""As entertaining as it is innovative, Toy Story kicked off Pixar's unprecedented run of quality pictures, reinvigorating animated film in the process."",""release_dates"":{""theater"":""1995-11-22"",""dvd"":""2001-03-20""},""ratings"":{""critics_rating"":""Certified Fresh"",""critics_score"":100,""audience_rating"":""Upright"",""audience_score"":81},""synopsis"":"""",""posters"":{""thumbnail"":""http://content7.flixster.com/movie/10/93/63/10936393_mob.jpg"",""profile"":""http://content7.flixster.com/movie/10/93/63/10936393_pro.jpg"",""detailed"":""http://content7.flixster.com/movie/10/93/63/10936393_det.jpg"",""original"":""http://content7.flixster.com/movie/10/93/63/10936393_ori.jpg""},""abridged_cast"":[{""name"":""Tom Hanks"",""id"":""162655641"",""characters"":[""Woody""]},{""name"":""Tim Allen"",""id"":""162655909"",""characters"":[""Buzz Lightyear""]},{""name"":""Don Rickles"",""id"":""341817905"",""characters"":[""Mr. Potato Head""]},{""name"":""Jim Varney"",""id"":""162662792"",""characters"":[""Slinky Dog""]},{""name"":""Wallace Shawn"",""id"":""162671862"",""characters"":[""Rex""]}],""alternate_ids"":{""imdb"":""0114709""},""links"":{""self"":""http://api.rottentomatoes.com/api/public/v1.0/movies/9559.json"",""alternate"":""http://www.rottentomatoes.com/m/toy_story/"",""cast"":""http://api.rottentomatoes.com/api/public/v1.0/movies/9559/cast.json"",""clips"":""http://api.rottentomatoes.com/api/public/v1.0/movies/9559/clips.json"",""reviews"":""http://api.rottentomatoes.com/api/public/v1.0/movies/9559/reviews.json"",""similar"":""http://api.rottentomatoes.com/api/public/v1.0/movies/9559/similar.json""}}],""links"":{""self"":""http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=toy+story&page_limit=3&page=1"",""next"":""http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=toy+story&page_limit=3&page=2""},""link_template"":""http://api.rottentomatoes.com/api/public/v1.0/movies.json?q={search-term}&page_limit={results-per-page}&page={page-number}""}"

    Dim obj As RootObject

    obj = JsonConvert.DeserializeObject(Of RootObject)(json)

End Sub
于 2013-06-02T21:15:13.447 回答