4

我刚刚开始使用 Rotten Tomatoes API 来检索电影信息,我需要一些帮助来了解如何处理返回的数据。这是我第一次使用这样的 API,所以如果这听起来很基础,请原谅我。

使用 cfhttp 我可以成功连接到 API 并返回搜索数据,但我真的不知道我返回的是什么格式。我以为是 JSON,但是使用 isJSON 检查它返回 false。我希望能够调用返回数据中的各个字段来填充我可以输出给用户的查询结果集。

我用来拨打电话的代码很简单:

<cfhttp url="#apiURL#movies.json?apikey=#apiKey#&q=#movieName#" method="get" result="httpResp" timeout="120">
    <cfhttpparam type="header" name="Content-Type" value="application/json" />
</cfhttp>
<cfdump var="#httpResp#" />

以及正在返回的数据:

这是数据返回时的样子

我不希望任何人给我一个完整的如何构建我的应用程序的演练,但如果有人能给我一些关于将数据转换为查询结果的正确方法的指示,或者我可以使用的其他东西,我会很感激。

编辑:没有意识到图像会如此难以阅读,所以这里是返回数据的剪切和粘贴。

{"total":2,"movies":[{"id":"11029","title":"Krull","year":1983,"mpaa_rating":"PG","runtime":120,"release_dates":{"theater":"1983-07-29","dvd":"2001-04-03"},"ratings":{"critics_rating":"Rotten","critics_score":33,"audience_rating":"Spilled","audience_score":49},"synopsis":"","posters":{"thumbnail":"http://content6.flixster.com/movie/25/86/258696_mob.jpg","profile":"http://content6.flixster.com/movie/25/86/258696_pro.jpg","detailed":"http://content6.flixster.com/movie/25/86/258696_det.jpg","original":"http://content6.flixster.com/movie/25/86/258696_ori.jpg"},"abridged_cast":[{"name":"Ken Marshall","id":"162668719","characters":["Prince Colwyn"]},{"name":"Lysette Anthony","id":"162668720","characters":["Lyssa"]},{"name":"Freddie Jones","id":"162664678","characters":["Ynyr"]},{"name":"Francesca Annis","id":"162688297","characters":["Widow of the Web"]},{"name":"Alun Armstrong","id":"770670461","characters":["Torquil"]}],"links":{"self":"http://api.rottentomatoes.com/api/public/v1.0/movies/11029.json","alternate":"http://www.rottentomatoes.com/m/krull/","cast":"http://api.rottentomatoes.com/api/public/v1.0/movies/11029/cast.json","clips":"http://api.rottentomatoes.com/api/public/v1.0/movies/11029/clips.json","reviews":"http://api.rottentomatoes.com/api/public/v1.0/movies/11029/reviews.json","similar":"http://api.rottentomatoes.com/api/public/v1.0/movies/11029/similar.json"}},{"id":"770670060","title":"Bekenntnisse des Hochstaplers Felix Krull (Confessions of Felix Krull)","year":1957,"mpaa_rating":"Unrated","runtime":107,"release_dates":{"theater":"1958-03-04"},"ratings":{"critics_score":-1,"audience_rating":"Spilled","audience_score":33},"synopsis":"","posters":{"thumbnail":"http://content7.flixster.com/movie/10/84/16/10841649_mob.jpg","profile":"http://content7.flixster.com/movie/10/84/16/10841649_pro.jpg","detailed":"http://content7.flixster.com/movie/10/84/16/10841649_det.jpg","original":"http://content7.flixster.com/movie/10/84/16/10841649_ori.jpg"},"abridged_cast":[{"name":"Horst Buchholz","id":"162718595","characters":["Felix Krull"]},{"name":"Liselotte Pulver","id":"326392065","characters":["Zaza"]},{"name":"Ingrid Andree","id":"770670669","characters":["Zouzou"]},{"name":"Susi Nicoletti","id":"770670670","characters":["Madame Houpfle"]},{"name":"Paul Dahlke","id":"573372814","characters":["Professor Kuckuck"]}],"alternate_ids":{"imdb":"0050179"},"links":{"self":"http://api.rottentomatoes.com/api/public/v1.0/movies/770670060.json","alternate":"http://www.rottentomatoes.com/m/bekenntnisse-des-hochstaplers-felix-krull-confessions-of-felix-krull/","cast":"http://api.rottentomatoes.com/api/public/v1.0/movies/770670060/cast.json","clips":"http://api.rottentomatoes.com/api/public/v1.0/movies/770670060/clips.json","reviews":"http://api.rottentomatoes.com/api/public/v1.0/movies/770670060/reviews.json","similar":"http://api.rottentomatoes.com/api/public/v1.0/movies/770670060/similar.json"}}],"links":{"self":"http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=Krull&page_limit=30&page=1"},"link_template":"http://api.rottentomatoes.com/api/public/v1.0/movies.json?q={search-term}&page_limit={results-per-page}&page={page-number}"}

编辑:谢谢,丹。那是我需要的轻推。在了解了如何获取 JSON 数据之后,我能够找到以下关于如何将其转化为有用查询的说明:Work with remote API JSON data in CF

4

2 回答 2

9

数据需要反序列化:

<cfset tomatoData=DeserializeJSON(httpResp.filecontent)>

<cfdump var="#tomatoData#">

看起来第一层只有结构。所以你也许可以

<cfdump var="#tomatoData.total#"> <!--- A single item --->
<cfdump var="#tomatoData.movies#"> <!--- An array --->
于 2013-11-10T19:39:40.270 回答
2

文件内容看起来像 json。您可以使用它来引用它

#httpResp.filecontent#.
于 2013-11-10T19:20:20.207 回答