Ruby-尝试使用嵌套的哈希遍历哈希(在对JSON对象进行反序列化之后)

问题描述 投票:1回答:1

我是Ruby的新手,我想我会写一个程序来调用API并反序列化JSON对象。

供参考,这是我正在调用的API; https://api.exchangeratesapi.io/latest?base=GBP

我在遍历哈希时遇到麻烦,出现以下错误:

Traceback (most recent call last):
        2: from ruby-api.rb:21:in `<main>'
        1: from ruby-api.rb:21:in `each'
ruby-api.rb:23:in `block in <main>': undefined method `each' for "GBP":String (NoMethodError)

这里是我到目前为止的代码(带注释)

#requiring the library for http client
require 'net/http'
#require the URI library
require 'uri'
#requiring the json library for deserialising JSON objects
require 'json'

#output of get request stored in a string to deserialise JSON
json_string = Net::HTTP.get(URI.parse('https://api.exchangeratesapi.io/latest?base=GBP'))

#testing output
puts "Direct output \n#{json_string}"

puts "First JSON Parse attempt\n"
JSON.parse(json_string).each do |currency, rate|
    puts "#{currency} - #{rate}"
end

#parse the JSON object to a hash and iterate through it, displaying each key, value pair
puts "JSON Parse output with nested each statements \n"
JSON.parse(json_string).each do |key, value|
    #however - this contains a nested hash! So a nested each do is needed.
    value.each do |currency, rate|
        puts "#{currency} - #{rate}"
    end
end

这是运行代码时的完整输出:

Direct output
{"rates":{"CAD":1.6409680499,"HKD":9.6691560447,"ISK":152.3948565332,"PHP":63.800325678,"DKK":8.3851984951,"HUF":373.6986916727,"CZK":28.9078555786,"GBP":1.0,"RON":5.3321354371,"SEK":12.1404907631,"IDR":17442.1809197597,"INR":87.4181593576,"BRL":5.0228536133,"RUB":79.8944353978,"HRK":8.3345499467,"JPY":131.652535235,"THB":37.5506766242,"CHF":1.2255601101,"EUR":1.1230276826,"MYR":5.16064911,"BGN":2.1964175417,"TRY":7.0194845303,"CNY":8.8154304004,"NOK":11.220731091,"NZD":1.9484530294,"ZAR":18.6923465663,"USD":1.2329720928,"MXN":24.1548655174,"SGD":1.7001516087,"AUD":1.824583076,"ILS":4.2960300971,"KRW":1473.7716884721,"PLN":4.8565332135},"base":"GBP","date":"2019-10-04"}
First JSON Parse attempt
rates - {"CAD"=>1.6409680499, "HKD"=>9.6691560447, "ISK"=>152.3948565332, "PHP"=>63.800325678, "DKK"=>8.3851984951, "HUF"=>373.6986916727, "CZK"=>28.9078555786, "GBP"=>1.0, "RON"=>5.3321354371, "SEK"=>12.1404907631, "IDR"=>17442.1809197597, "INR"=>87.4181593576, "BRL"=>5.0228536133, "RUB"=>79.8944353978, "HRK"=>8.3345499467, "JPY"=>131.652535235, "THB"=>37.5506766242, "CHF"=>1.2255601101, "EUR"=>1.1230276826, "MYR"=>5.16064911, "BGN"=>2.1964175417, "TRY"=>7.0194845303, "CNY"=>8.8154304004, "NOK"=>11.220731091, "NZD"=>1.9484530294, "ZAR"=>18.6923465663, "USD"=>1.2329720928, "MXN"=>24.1548655174, "SGD"=>1.7001516087, "AUD"=>1.824583076, "ILS"=>4.2960300971, "KRW"=>1473.7716884721, "PLN"=>4.8565332135}
base - GBP
date - 2019-10-04
JSON Parse output with nested each statements
CAD - 1.6409680499
HKD - 9.6691560447
ISK - 152.3948565332
PHP - 63.800325678
DKK - 8.3851984951
HUF - 373.6986916727
CZK - 28.9078555786
GBP - 1.0
RON - 5.3321354371
SEK - 12.1404907631
IDR - 17442.1809197597
INR - 87.4181593576
BRL - 5.0228536133
RUB - 79.8944353978
HRK - 8.3345499467
JPY - 131.652535235
THB - 37.5506766242
CHF - 1.2255601101
EUR - 1.1230276826
MYR - 5.16064911
BGN - 2.1964175417
TRY - 7.0194845303
CNY - 8.8154304004
NOK - 11.220731091
NZD - 1.9484530294
ZAR - 18.6923465663
USD - 1.2329720928
MXN - 24.1548655174
SGD - 1.7001516087
AUD - 1.824583076
ILS - 4.2960300971
KRW - 1473.7716884721
PLN - 4.8565332135
Traceback (most recent call last):
        2: from ruby-api.rb:21:in `<main>'
        1: from ruby-api.rb:21:in `each'
ruby-api.rb:23:in `block in <main>': undefined method `each' for "GBP":String (NoMethodError)

所以发生的事情是,它将很高兴地遍历哈希,但是一旦它击中了'base'键(值为GBP),它就会崩溃。

解决此问题的最佳方法是什么?谢谢!

json ruby loops hash
1个回答
0
投票

您正在尝试遍历解析的JSON中的每个值,因为rates的值是一个哈希值,但是basedate的值是字符串,所以它可以同时工作。在此调用each并获取NoMethodError异常。

尝试访问dates键:

JSON.parse(json_string)['rates'].each do |currency, rate|
  puts "#{currency} - #{rate}"
end
© www.soinside.com 2019 - 2024. All rights reserved.