无法在 Ruby 中提取单个 JSON 值
Trouble extracting individual JSON values in Ruby
我正在尝试抓取 reddit (API-free),但我 运行 碰壁了。在 reddit 上,每个页面都有一个 JSON 表示,只需在末尾附加 .json
即可看到,例如https://www.reddit.com/r/AskReddit.json
.
我安装了 NeatJS,并编写了一小段代码来清理 JSON 并打印它:
require "rubygems"
require "json"
require "net/http"
require "uri"
require 'open-uri'
require 'neatjson'
url = ("https://www.reddit.com/r/AskReddit.json")
result = JSON.parse(open(url).read)
neatJS = JSON.neat_generate(result, wrap: 40, short: true, sorted: true, aligned: true, aroundColonN: 1)
puts neatJS
而且效果很好:
(还有更多的方法,它继续了另外几页,完整的 JSON 在这里:http://pastebin.com/HDzFXqyU)
但是,当我将其更改为仅提取我想要的值时:
url = ("https://www.reddit.com/r/AskReddit.json")
result = JSON.parse(open(url).read)
neatJS = JSON.neat_generate(result, wrap: 40, short: true, sorted: true, aligned: true, aroundColonN: 1)
neatJS.each do |data|
puts data["title"]
puts data["url"]
puts data["id"]
end
它给了我一个错误:
002----extractallaskredditthreads.rb:17:in `<main>': undefined method `each' for #<String:0x0055f948da9ae8> (NoMethodError)
我已经尝试了大约两天的提取器的不同变体,其中 none 已经奏效了。我觉得我错过了一些非常明显的东西。如果有人能指出我做错了什么,那将不胜感激。
编辑
原来我弄错了变量名:
neatSJ =/= neatJS
但是,更正它只会改变我得到的错误:
002----extractallaskredditthreads.rb:17:in `<main>': undefined method `each' for #<String:0x0055f948da9ae8> (NoMethodError)
正如我所说,我一直在尝试多种提取标签的方法,这可能导致了我的打字错误。
是否打错了?
neatJS = JSON.neat_generate
[...]
neatSJ.each do |data|
在此代码中:
result = JSON.parse(open(url).read)
neatJS = JSON.neat_generate(result, wrap: 40, short: true, sorted: true, aligned: true, aroundColonN: 1)
...result
是一个Ruby Hash 对象,将JSON 解析为Ruby 对象与JSON.parse
的结果。同时,neatJS
是一个字符串,是在 result
哈希上调用 JSON.neat_generate
的结果。在字符串上调用 each
没有意义。如果你想访问 JSON 结构中的值,你想使用 result
对象,而不是 neatJS
字符串:
children = result["data"]["children"]
children.each do |child|
puts child["data"]["title"]
puts child["data"]["url"]
puts child["data"]["id"]
end
我正在尝试抓取 reddit (API-free),但我 运行 碰壁了。在 reddit 上,每个页面都有一个 JSON 表示,只需在末尾附加 .json
即可看到,例如https://www.reddit.com/r/AskReddit.json
.
我安装了 NeatJS,并编写了一小段代码来清理 JSON 并打印它:
require "rubygems"
require "json"
require "net/http"
require "uri"
require 'open-uri'
require 'neatjson'
url = ("https://www.reddit.com/r/AskReddit.json")
result = JSON.parse(open(url).read)
neatJS = JSON.neat_generate(result, wrap: 40, short: true, sorted: true, aligned: true, aroundColonN: 1)
puts neatJS
而且效果很好:
但是,当我将其更改为仅提取我想要的值时:
url = ("https://www.reddit.com/r/AskReddit.json")
result = JSON.parse(open(url).read)
neatJS = JSON.neat_generate(result, wrap: 40, short: true, sorted: true, aligned: true, aroundColonN: 1)
neatJS.each do |data|
puts data["title"]
puts data["url"]
puts data["id"]
end
它给了我一个错误:
002----extractallaskredditthreads.rb:17:in `<main>': undefined method `each' for #<String:0x0055f948da9ae8> (NoMethodError)
我已经尝试了大约两天的提取器的不同变体,其中 none 已经奏效了。我觉得我错过了一些非常明显的东西。如果有人能指出我做错了什么,那将不胜感激。
编辑
原来我弄错了变量名:
neatSJ =/= neatJS
但是,更正它只会改变我得到的错误:
002----extractallaskredditthreads.rb:17:in `<main>': undefined method `each' for #<String:0x0055f948da9ae8> (NoMethodError)
正如我所说,我一直在尝试多种提取标签的方法,这可能导致了我的打字错误。
是否打错了?
neatJS = JSON.neat_generate
[...]
neatSJ.each do |data|
在此代码中:
result = JSON.parse(open(url).read)
neatJS = JSON.neat_generate(result, wrap: 40, short: true, sorted: true, aligned: true, aroundColonN: 1)
...result
是一个Ruby Hash 对象,将JSON 解析为Ruby 对象与JSON.parse
的结果。同时,neatJS
是一个字符串,是在 result
哈希上调用 JSON.neat_generate
的结果。在字符串上调用 each
没有意义。如果你想访问 JSON 结构中的值,你想使用 result
对象,而不是 neatJS
字符串:
children = result["data"]["children"]
children.each do |child|
puts child["data"]["title"]
puts child["data"]["url"]
puts child["data"]["id"]
end