ruby - 为什么我会得到 "no implicit conversion of String i

我有一些代码在非常相似的情况下运行。这是第一种情况,我有一个电影的 imdb_id 我想要详细信息:

url = "http://mymovieapi.com/?id=#{self.imdb_id}&type=json&plot=none&episode=0&lang=en-US&aka=simple&release=simple&business=0&tech=0"
doc = Hpricot(open(url)).to_s
json = JSON.parse(doc)

puts json
puts json["imdb_id"]

这给出了以下结果:

{"rating_count"=>493949,
"genres"=>["Drama", "Romance"],
"rated"=>"PG-13",
"language"=>["English", "French", "German", "Swedish", "Italian", "Russian"],
"rating"=>7.6,
"country"=>["USA"],
"release_date"=>19980403,
"title"=>"Titanic",
"year"=>1997,
"filming_locations"=>"Santa Clarita, California, USA",
"imdb_id"=>"tt0120338",
"directors"=>["James Cameron"],
"writers"=>["James Cameron"],
"actors"=>["Leonardo DiCaprio", "Kate Winslet", "Billy Zane", "Kathy Bates", "Frances Fisher", "Gloria Stuart", "Bill Paxton", "Bernard Hill", "David Warner", "Victor Garber", "Jonathan Hyde", "Suzy Amis", "Lewis Abernathy", "Nicholas Cascone", "Anatoly M. Sagalevitch"],
"also_known_as"=>["Tai tan ni ke hao"],
"poster"=>{"imdb"=>"http://ia.media-imdb.com/images/M/MV5BMjExNzM0NDM0N15BMl5BanBnXkFtZTcwMzkxOTUwNw@@._V1_SY317_CR0,0,214,317_.jpg", "cover"=>"http://imdb-poster.b0.upaiyun.com/000/120/338.jpg!cover?_upt=66ac07591382594194"},
"runtime"=>["194 min"],
"type"=>"M",
"imdb_url"=>"http://www.imdb.com/title/tt0120338/"}

tt0120338

这符合预期。第二种情况,我有同一部电影的titleyear:

url = "http://mymovieapi.com/?title=#{self.title}&type=json&plot=simple&episode=0&limit=1&year=#{self.year}&yg=1&mt=none&lang=en-US&offset=&aka=simple&release=simple&business=0&tech=0"
doc = Hpricot(open(url)).to_s
json = JSON.parse(doc)

puts json
puts json["imdb_id"]

由此,我得到完全相同的 JSON 输出:

{"rating_count"=>493949,
"genres"=>["Drama", "Romance"],
"rated"=>"PG-13", "language"=>["English", "French", "German", "Swedish", "Italian", "Russian"],
"rating"=>7.6,
"country"=>["USA"],
"release_date"=>19980403,
"title"=>"Titanic",
"year"=>1997,
"filming_locations"=>"Santa Clarita, California, USA",
"imdb_id"=>"tt0120338",
"directors"=>["James Cameron"],
"writers"=>["James Cameron"],
"actors"=>["Leonardo DiCaprio", "Kate Winslet", "Billy Zane", "Kathy Bates", "Frances Fisher", "Gloria Stuart", "Bill Paxton", "Bernard Hill", "David Warner", "Victor Garber", "Jonathan Hyde", "Suzy Amis", "Lewis Abernathy", "Nicholas Cascone", "Anatoly M. Sagalevitch"],
"also_known_as"=>["Tai tan ni ke hao"],
"poster"=>{"imdb"=>"http://ia.media-imdb.com/images/M/MV5BMjExNzM0NDM0N15BMl5BanBnXkFtZTcwMzkxOTUwNw@@._V1_SY317_CR0,0,214,317_.jpg",
"cover"=>"http://imdb-poster.b0.upaiyun.com/000/120/338.jpg!cover?_upt=ec8bdec31382594417"},
"runtime"=>["194 min"],
"type"=>"M",
"imdb_url"=>"http://www.imdb.com/title/tt0120338/"}

但是当我尝试调用 puts json["imdb_id"] 时,我得到了这个错误:

no implicit conversion of String into Integer (TypeError)

使用 titleyear 获取时总是会发生这种情况,但似乎无法解释,因为 JSON 输出完全相同。

最佳答案

从异常看来,第二个响应中的json似乎是一个只有一个元素的数组,所以puts json的输出是一样的(括号不要' t 用 puts) 获得输出,但 json["string"] 失败,因为 [] 期望 Integer用作索引。

检查 p json 或那个 json.is_a?(Array) 如果它确实是一个数组,请尝试使用 json.first['imdb_id'] .

关于ruby - 为什么我会得到 "no implicit conversion of String into Integer (TypeError)"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19549098/

相关文章:

json - 在jq中将字符串转换为json

java - 如何在 Java 的 JSON 字符串中找到指定的名称及其值?

objective-c - 使用 Json 在 Objective C 中发布数据

json - Golang 将 JSON 数组解析成数据结构

python - 使用 simplejson 序列化简单类对象的最简单方法?

ios - (Cocoa 错误 3840。)“(字符 0 周围的值无效。)AFNetworking

c# - 强制 JSON.NET 在序列化 DateTime 时包含毫秒(即使 ms 组件为零)

ios - 如何在 Swift 中解析 JSON 中的数组?

javascript - 短语对象文字表示法中的 "literal"是什么意思?

python - 使用 JSON 模块进行 pretty-print 时如何实现自定义缩进?