require "rubygems"
require "json"
require "net/http"
require "uri"
uri = URI.parse("https://api.website.com/top/inside/end")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
http.use_ssl = true
response = http.request(request)
if response.code == "200"
result = JSON.parse(response.body)
names = Array.new
i = 0
result.each do |doc|
names.insert(i, doc.name)
puts doc["id"] #reference properties like this
puts doc # this is the result in object form
puts ""
puts ""
end
puts names
else
puts "ERROR!!!"
end
为什么我在每个循环中的变量名称上得到未定义的方法错误?我无法理解为什么
script.rb:18:在`block in'中:未定义的方法`name'用于#< Hash:0x0000000002e04c08> (NoMethodError)
答案 0 :(得分:2)
第18行,
names.insert(i, doc.name)
doc是一个哈希,doc['name']
应该有效。
在javascript中,你可以做到
doc.name