将 json 数据导入 redis 中



我是 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、标题、内容和 put 项目(现在返回整个对象(和键中的 json[item] 数据有什么帮助吗? 如果我尝试例如

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

再次no implicit conversion of String into Integer

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

我在这里看到两个问题:

1.

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

item是哈希。 json[item]对我来说毫无意义。

阿拉伯数字。

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

应该在 Redis 哈希'latest_posts'中将键item设置为值 item['title'] 。但是item是一个哈希值,我认为 Redis 希望有一个字符串作为归档名称。

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

最新更新