我正在尝试编写一个 Ruby 程序,该程序将从 Json 文件中检索所有 reddit 用户名。我可以让它显示列表,但是每次第一个用户名之后都会出错。
require 'net/http'
require 'rubygems'
require 'json'
require 'pp'
@response = Net::HTTP.get(URI.parse("http://www.reddit.com/r/AskReddit/comments/sl1nn /could_codeine_help_me_sleep_is_it_dangerous/.json"))
result = JSON.parse(@response)
comments = result[1]['data']['children'] #this is now an array of comment hashes
(0..comments.length).each do |i|
comment = comments[i]['data']
puts comment['author']
end
虽然它显示列表,但我也收到此错误:
在block in <main>': undefined method
[]' 中为 nil:NilClass (NoMethodError)
有谁知道我可以解决这个问题?