2013-07-18 20 views
0

我是新来的Redis和红宝石我使用下面的代码导入JSON数据为Redis的

response = HTTParty.get('posts.json') 
json = JSON(response.body) 

json.each do |item| 
    puts item 
    puts json[item] 
    puts item['content'] 
    redis.hset 'latest_posts', item, item['title'] 
end 

puts json[item] 

返回以下error no implicit conversion of Hash into Integer

每个循环中我要输出例如ID键,标题,内容与放置项目(现在返回整个对象)和JSON的[项目]数据的任何帮助?如果我尝试例如

%w{id title content}.each do |key| 
     puts key 
     puts json[key] 
end 

再次no implicit conversion of String into Integer

+0

什么是'json'分配给?你确定你使用的是正确的吗?通常你会看到'JSON.load(response.body)'。 – tadman

+0

我使用HTTParty来加载json是一个变量分配给json对象只是一个名字 – sger

+0

你应该'把json.inspect'放在第三行以确保它是正确的类型。当你调用字符串或数组上的'[]'时,你得到的错误往往会发生什么。同样'把json [item] .inspect'放在循环中。 – tadman

回答

0

从你的代码似乎你希望哈希在你的JSON数组。

我在这里看到了两个问题:

1.

puts json[item] 
puts item['content'] 

item是一个哈希值。 json[item]对我没有意义。

2.

redis.hset 'latest_posts', item, item['title'] 

应该设置在Redis的散列'latest_posts'密钥item为值item['title']。但item是一个散列,我认为,Redis需要一个字符串作为提交的名称。

顺便说一句,是不是JSON.parse response.body