2013-06-02 34 views
0

我試圖讓用戶輸入一個電影標題到我的程序,然後讓程序返回爛番茄數據庫中的前三個匹配電影。VB&爛番茄JSON API

這裏是我到目前爲止的代碼:

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}" 
} 

回答

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 
相關問題