我做错了什么? 我可以重复执行Array的前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,“名称” =>“比特币”,“符号” =>“ BTC”,“ website_slug” =>“比特币”,“等级” => 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,“名称” =>“以太坊”,“符号” =>“ 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,”名称“ =>” XRP“,”符号“ = >“ XRP”,“ website_slug” =>“ ripple”,“ rank” => 3,“ circulating_supply” => 39870907279.0,“ total_supply” => 99991836919.0,“ max_supply” => 100000000000.0,“ quotes” => {“ USD “ => {”价格“ => 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,“名称” =>“比特币现金”,“符号” =>“ BCH”,“ website_slug” =>“比特币现金” ,“ 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,“名称” =>“ Stellar”,“符号” =>“ 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,“名称” =>“ Litecoin”,“符号” =>“ 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}},
答案 0 :(得分:1)
您可以尝试此代码并反馈:)
<h1>LISTING </h1>
<% @jsonData.fetch('data', {}).each do |id, coin| %>
Name: <%= coin['name'] %> <br />
Symbol: <%= coin['symbol'] %> <br />
Website_slug: <%= coin['website_slug'] %> <br />
Rank: <%= coin['rank'] %> <br />
Circulating_supply: <%= coin['circulating_supply'] %> <br />
total_supply: <%= coin['total_supply'] %> <br />
max_supply: <%= coin['max_supply'] %> <br />
Price: <%= coin.dig('quotes', 'USD', 'price') %> <br />
Volume24h: <%= coin.dig('quotes', 'USD', 'volume_24h') %> <br />
Market_cap: <%= coin.dig('quotes', 'USD', 'market_cap') %> <br />
Change_1he: <%= coin.dig('quotes', 'USD', 'pricpercent_change_1he') %> <br />
Change_24h: <%= coin.dig('quotes', 'USD', 'percent_change_24h') %> <br />
Change_7d: <%= coin.dig('quotes', 'USD', 'percent_change_7d') %> <br />
<%end%>
<%end%>
这是您的JSON示例:
{
"data":{
"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":null,
"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":null,
"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":null,
"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
}
}
}
}
}
在此json之后,您可以看到没有数组[]
,而只是嵌套的哈希{}
,因此不需要所有循环。对于前。要获得价格,您需要执行以下操作:@jsonData['data']['1']['quotes']['USD']['price']
此路径['data']['1']['quotes']['USD']['price']
都是静态的,这意味着每个coin
都相同。唯一的动态/变化部分是['1']
。有时是['1']
,['1027']
,['52']
等。
因此,在我的代码中,您可以看到我静态地访问了所有数据,但只访问了IDs
我必须对其进行循环,但是此循环将结束key => value
,因为如前所述处理哈希而不是数组。
所以当我做<% @jsonData.fetch('data', {}).each do |id, coin| %>
时:
id
的值为'1'
。coin
具有硬币的结构(例如,您可以静态检索诸如coin.dig('quotes', 'USD', 'price')
之类的任何数据。您在做什么:
<% @jsonData.['data'].each do |coin| %>
:
coin
将是['1', 'structure of the coin here']
coin['name']
时,它将返回nil,但不会失败。在这一点上,您认为它正在工作,但是没有。