0

我是 redis 和 ruby​​ 的新手,我正在使用以下代码

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、title、content 和 puts item(现在返回整个对象)和 key 中的 json[item] 数据有什么帮助吗?例如,如果我尝试

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

再次no implicit conversion of String into Integer

4

1 回答 1

0

从您的代码看来,您似乎期望 JSON 中有一个哈希数组。

我在这里看到两个问题:

1.

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

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

2.

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

应该将itemRedis 哈希'latest_posts'中的 key 设置为 value item['title']。Butitem是一个哈希,我认为,Redis 需要一个字符串作为文件名。

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

于 2013-07-19T01:21:24.917 回答