我是 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]
返回以下错误 no implicit conversion of Hash into Integer
在每个循环中,我想输出键,例如 id、title、content with puts item(现在返回整个对象)和键中的 json[item] 数据有什么帮助吗?例如,如果我尝试
%w{id title content}.each do |key|
puts key
puts json[key]
end
同样 没有将 String 隐式转换为 Integer
最佳答案
从您的代码看来,您似乎希望在 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
吗?
关于ruby - 将json数据导入redis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17733818/