我将如何解析 Ruby 中的 JSON 以提供特定的输出
How would I parse JSON in Ruby to give me specific output
所以我试图在 Ruby 中添加,以便它“不知道如何表达自己”从这个 API 解析 JSON 所以输出是:
{"infected"=>19334, "deceased"=>429, "recovered"=>14047, "tested"=>515395, "tested24hours"=>8393, "infected24hours"=>351, "deceased24hours"=>11, "sourceUrl"=>"https://covid19.rs/homepage-english/", "lastUpdatedAtApify"=>"2020-07-15T14:00:00.000Z", "readMe"=>"https://apify.com/krakorj/covid-serbia"}
我希望它只显示例如 "infected"=>19334,
作为数字 19334
我是 Ruby 编程的新手我仍在学习它,因为它是 COVID 大流行和锁定我有更多的空闲时间,做一些与之相关的东西有点有意义。
这是我到目前为止所做的:
require 'httparty'
require 'json'
url = 'https://api.apify.com/v2/key-value-stores/aHENGKUPUhKlX97aL/records/LATEST?disableRedirect=true'
response = HTTParty.get(url)
re = response.parsed_response
puts re
当然,你这样做:
re["infected"]
=> 19334
HTTPParty 解析响应 returns 一个散列,因此您可以通过键访问该值。如果键是“字符串”,则您使用“字符串”进行访问。如果他们的键是 :symbol,您可以使用 :symbol 访问。
不完全确定这是否是您的问题,但是 parsed_response
已经解析了 json 并将其转换为散列。
因此,要访问 infected
字段,您只需执行
re = response.parsed_response
infected = re["infected"]
puts infected
个人而言,我更喜欢使用 'open-uri'。
require 'open-uri'
require 'json'
url = 'https://api.apify.com/v2/key-value-stores/aHENGKUPUhKlX97aL/records/LATEST?disableRedirect=true'
response = open(url)
json = JSON.parse(response)
puts json["infected"]
-> 输出 19334
require 'httparty'
require 'json'
url = 'https://api.apify.com/v2/key-value-
stores/aHENGKUPUhKlX97aL/records/LATEST?disableRedirect=true'
response = HTTParty.get(url)
re = response.parsed_response
在这个阶段我们有那个输出
{"infected"=>19334, "deceased"=>429, "recovered"=>14047, "tested"=>515395, "tested24hours"=>8393, "infected24hours"=>351, "deceased24hours"=>11, "sourceUrl"=>"https://covid19.rs/homepage-english/", "lastUpdatedAtApify"=>"2020-07-15T14:10:00.000Z", "readMe"=>"https://apify.com/krakorj/covid-serbia"}
您可以像这样访问以上任何变量值
infected = re["infected"]
deceased = re["deceased"]
等等..
所以我试图在 Ruby 中添加,以便它“不知道如何表达自己”从这个 API 解析 JSON 所以输出是:
{"infected"=>19334, "deceased"=>429, "recovered"=>14047, "tested"=>515395, "tested24hours"=>8393, "infected24hours"=>351, "deceased24hours"=>11, "sourceUrl"=>"https://covid19.rs/homepage-english/", "lastUpdatedAtApify"=>"2020-07-15T14:00:00.000Z", "readMe"=>"https://apify.com/krakorj/covid-serbia"}
我希望它只显示例如 "infected"=>19334,
作为数字 19334
我是 Ruby 编程的新手我仍在学习它,因为它是 COVID 大流行和锁定我有更多的空闲时间,做一些与之相关的东西有点有意义。
这是我到目前为止所做的:
require 'httparty'
require 'json'
url = 'https://api.apify.com/v2/key-value-stores/aHENGKUPUhKlX97aL/records/LATEST?disableRedirect=true'
response = HTTParty.get(url)
re = response.parsed_response
puts re
当然,你这样做:
re["infected"]
=> 19334
HTTPParty 解析响应 returns 一个散列,因此您可以通过键访问该值。如果键是“字符串”,则您使用“字符串”进行访问。如果他们的键是 :symbol,您可以使用 :symbol 访问。
不完全确定这是否是您的问题,但是 parsed_response
已经解析了 json 并将其转换为散列。
因此,要访问 infected
字段,您只需执行
re = response.parsed_response
infected = re["infected"]
puts infected
个人而言,我更喜欢使用 'open-uri'。
require 'open-uri'
require 'json'
url = 'https://api.apify.com/v2/key-value-stores/aHENGKUPUhKlX97aL/records/LATEST?disableRedirect=true'
response = open(url)
json = JSON.parse(response)
puts json["infected"]
-> 输出 19334
require 'httparty'
require 'json'
url = 'https://api.apify.com/v2/key-value-
stores/aHENGKUPUhKlX97aL/records/LATEST?disableRedirect=true'
response = HTTParty.get(url)
re = response.parsed_response
在这个阶段我们有那个输出
{"infected"=>19334, "deceased"=>429, "recovered"=>14047, "tested"=>515395, "tested24hours"=>8393, "infected24hours"=>351, "deceased24hours"=>11, "sourceUrl"=>"https://covid19.rs/homepage-english/", "lastUpdatedAtApify"=>"2020-07-15T14:10:00.000Z", "readMe"=>"https://apify.com/krakorj/covid-serbia"}
您可以像这样访问以上任何变量值
infected = re["infected"]
deceased = re["deceased"]
等等..