json - 我将如何在 Ruby 中解析 JSON 以提供特定的输出
问题描述
所以我试图在 Ruby 中制作,以便它“Idk 如何表达自己”从这个 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
解决方案
不完全确定这是否是您的问题,但parsed_response
已经解析了 json 并将其转换为哈希。
所以要访问该infected
领域,你可以这样做
re = response.parsed_response
infected = re["infected"]
puts infected
推荐阅读
- node.js - NodeJS 的 OWL 推理器
- html - 为什么我不能摆脱下拉按钮中的插入符号?
- ejb - 检查 MDB 是否正在运行
- powershell - Powershell - 将结果加在一起
- swift - SwiftUI - 安排通知在特定日期后重复
- firebase - Firebase 模拟器警告,根据 GCLOUD_PROJECT 估计 Firebase 配置。初始化 firebase-admin 可能会失败
- pattern-matching - 酉矩阵是如何排列的?- 量子模式匹配
- x86 - 技嘉主板上的 GPIO 编程
- javascript - Node forEach 的解决方案
- jquery - 找到时jQuery更新数组中的对象?