【发布时间】:2019-03-06 11:16:20
【问题描述】:
我做错了什么? 数组的前 7 行我可以迭代,但是当我尝试打开另一个维度时,我会收到错误。
我的代码
require 'rest-client'
class ApiController < ApplicationController
def listings
url = 'https://api.coinmarketcap.com/v2/ticker/'
response = RestClient.get(url)
@jsonData = JSON.parse(response)
end
end
api_controller 工作
<h1>LISTING </h1>
<% @jsonData["data"].each do |coin| %>
<%coin.each do |id| %>
Name: <%= id["name"] %> <br />
Symbol: <% id["symbol"] %><br />
Website_slug: <% id["website_slug"] %><br />
Rank: <% id["rank"] %><br />
Circulating_supply: <% id["circulating_supply"] %><br />
total_supply: <% id["total_supply"] %><br />
max_supply: <% id["max_supply"] %><br />
到这里就可以了
从这里开始它不起作用
<%id["quotes"].each do |quotes| %>
<%quotes.each do |usd| %>
Price: <% usd["price"] %><br />
Volume24h: <%= usd["volume_24h"] %><br />
Market_cap: <%= usd["market_cap"] %><br />
Change_1he: <%= usd["pricpercent_change_1he"] %><br />
Change_24h: <%= usd["percent_change_24h"] %><br />
Change_7d: <%= usd["percent_change_7d"] %><br />
<%end%>
<%end%>
<%end%>
<%end%>
{"1"=>{"id"=>1, "name"=>"Bitcoin", "symbol"=>"BTC", "website_slug"=>"bitcoin", "rank"=>1 , "circulating_supply"=>17298850.0, "total_supply"=>17298850.0, "max_supply"=>21000000.0, "quotes"=>{"USD"=>{"price"=>6592.56514166, "volume_24h"=>3990509859.95985, "market_cap "=>114043795501.0, "percent_change_1h"=>-0.31, "percent_change_24h"=>-0.36, "percent_change_7d"=>-0.61}}, "last_updated"=>1538393012}, "1027"=>{"id"=> 1027, "name"=>"Ethereum", "symbol"=>"ETH", "website_slug"=>"ethereum", "rank"=>2, "circulating_supply"=>102298658.0, "total_supply"=>102298658.0, "max_supply"=>nil, "quotes"=>{"USD"=>{"price"=>229.147840966, "volume_24h"=>1701049487.13546, "market_cap"=>23441516679.0, "percent_change_1h"=>-0.8, "percent_change_24h "=>-2.56, "percent_change_7d"=>-2.44}}, "last_updated"=>1538393021}, "52"=>{"id"=>52, "name"=>"XRP", "symbol"= >"XRP", "website_slug"=>"ripple", "rank"=>3, "circulating_supply"=>39870907279.0, "total_supply"=>99991836919.0, "max_supply"=>100000000000.0, "quotes"=>{"USD "=>{"price"=>0.5790784377, "volume_24h"=>1229031882.79177, "market_cap"=>23088382697.0, "percent_change_1h"=>-1.6, "percent_change_24h"=>-4.58, "percent_change_7d"=>7.96}}, "last_updated"=>1538393043}, "1831"=>{"id"=>1831, "name"=>"Bitcoin Cash", "symbol"=>"BCH", "website_slug"=>"bitcoin-cash" , "rank"=>4, "circulating_supply"=>17378475.0, "total_supply"=>17378475.0, "max_supply"=>21000000.0, "quotes"=>{"USD"=>{"price"=>532.361000338, "volume_24h "=>480935803.85542, "market_cap"=>9251622335.0, "percent_change_1h"=>-0.78, "percent_change_24h"=>-1.57, "percent_change_7d"=>13.15}}, "last_updated"=>1538393013}, "1765"=> {"id"=>1765, "name"=>"EOS", "symbol"=>"EOS", "website_slug"=>"eos", "rank"=>5, "circulating_supply"=>906245118.0, " total_supply"=>1006245120.0, "max_supply"=>nil, "quotes"=>{"USD"=>{"price"=>5.6494577239, "volume_24h"=>822687391.374753, "market_cap"=>5119793479.0, "percent_change_1h"= >-0.53, "percent_change_24h"=>-3.01, "percent_change_7d"=>-1.66}}, "last_updated"=>1538393011} , "512"=>{"id"=>512, "name"=>"Stellar", "symbol"=>"XLM", "website_slug"=>"stellar", "rank"=>6, "circulating_supply "=>18789958255.0, "total_supply"=>104323820467.0, "max_supply"=>nil, "quotes"=>{"USD"=>{"price"=>0.261922298, "volume_24h"=>67633971.9905846, "market_cap"=> 4921509046.0, "percent_change_1h"=>0.14, "percent_change_24h"=>1.03, "percent_change_7d"=>-0.49}}, "last_updated"=>1538392997}, "2"=>{"id"=>2, "name" =>"Litecoin", "symbol"=>"LTC", "website_slug"=>"litecoin", "rank"=>7, "circulating_supply"=>58532552.0, "total_supply"=>58532552.0, "max_supply"=> 84000000.0, "quotes"=>{"USD"=>{"price"=>60.7806370254, "volume_24h"=>461333051.026648, "market_cap"=>3557645805.0, "percent_change_1h"=>-0.07, "percent_change_24h"=>-1.17 , "percent_change_7d"=>4.09}},
【问题讨论】:
-
你能在
@jsonData = JSON.parse(response)之后做raise @jsonData["data"].inspect并将输出粘贴到问题中吗? -
我得到了错误:ApiController#listings 中的 RuntimeError 并列出了所有的硬币/代币
-
是的,复制并粘贴到问题中。
-
我做了,但只是一部分,因为它有超过 40'000 个标志,我只能用 30'000 发布
标签: ruby-on-rails multidimensional-array each