【发布时间】:2017-06-18 11:02:51
【问题描述】:
我在显示来自 api 的 json 响应中的所有值时遇到了一点问题,尤其是在一些值位于数组中并且其中包含数组的 rullings 部分。
我在显示数据方面取得了一些成功,但在涉及数组时我很挣扎
我搜索了许多相关问题,但许多似乎都专注于生成 json 以显示它
以下是我迄今为止所做的,希望我说得通:
控制器:
class MtgController < ApplicationController
require 'net/http'
require 'json'
layout 'admin'
def index
@message = "Mtg Api"
if params[:search].present?
url = 'https://api.magicthegathering.io/v1/cards?name='+ params[:search]
uri = URI(url)
responce= Net::HTTP.get(uri)
@json = JSON.parse(responce)
end
end
end
查看
<% @page_title = "Magic the Gathering" %>
<h1>Magic the Gathering</h1>
<div class="search">
<%= form_tag(mtg_index_path, :method => :post) do %>
<table>
<tr>
<td><%= label_tag(:search) %></td>
<td><%= text_field_tag(:search, params[:search]) %></td>
</tr>
<td> </td>
<td><%= submit_tag("search") %></td>
</tr>
</table>
<% end %>
<%
if params[:search].present?
@json["cards"].each do |json|
%>
<p>
Name <%= json['name'] %><br>
manaCost <%= json['manaCost'] %><br>
cmc <%= json['cmc'] %><br>
colors <%= json['colors'] %><br>
colorIdentity <%= json['colorIdentity'] %><br>
type <%= json['type'] %><br>
types <%= json['types'] %><br>
subtypes <%= json['subtypes'] %><br>
rarity <%= json['rarity'] %><br>
set <%= json['set'] %><br>
setName <%= json['setName'] %><br>
text <%= json['text'] %><br>
artist <%= json['artist'] %><br>
power <%= json['power'] %><br>
toughness <%= json['toughness'] %><br>
layout <%= json['layout'] %><br>
multiverseid <%= json['multiverseid'] %><br>
imageUrl <%= json['imageUrl'] %><br>
originalText <%= json['originalText'] %><br>
rulings: <br>
<%= json['rulings'] %><br>
<% json['legalities'].each do |play|%>
legality <%= play['format'] %> <%= play['legality'] %><br>
<% end %>
id <%= json['id'] %><br>
</p>
<% end %>
<% end %>
</div>
Json 响应:
{"cards":[
{
"name":"Ball Lightning",
"manaCost":"{R}{R}{R}",
"cmc":3,
"colors":["Red"],
"colorIdentity":["R"],
"type":"Creature — Elemental",
"types":["Creature"],
"subtypes":["Elemental"],
"rarity":"Rare",
"set":"DRK",
"setName":"The Dark",
"text":"Trample (This creature can deal excess combat damage to defending player or planeswalker while attacking.)\nHaste (This creature can attack and {T} as soon as it comes under your control.)\nAt the beginning of the end step, sacrifice Ball Lightning.",
"artist":"Quinton Hoover",
"power":"6",
"toughness":"1",
"layout":"normal",
"multiverseid":1783,
"imageUrl":"http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=1783&type=card",
"rulings":[
{
"date":"2007-02-01",
"text":"The creature is sacrificed at the end of every turn in which it is on the battlefield. There is no choice about what turn to sacrifice it."}],
"printings":["DRK","4ED","5ED","pJGP","BTD","MED","M10","PD2"],
"originalText":"Trample\nBall Lightning may attack on the turn during which it is summoned. Ball Lightning is buried at the end of the turn during which it is summoned.",
"originalType":"Summon — Ball Lightning",
"legalities":[
{"format":"Commander","legality":"Legal"},
{"format":"Legacy","legality":"Legal"},
{"format":"Modern","legality":"Legal"},
{"format":"Vintage","legality":"Legal"}
],
"id":"d7e1cba15888f4a6e82081a4c14123136fb9eb85"
}
]}
【问题讨论】:
-
我很困惑——如果你在服务器端渲染,为什么要转换成 json?
-
我是吗?据我所知,我正在向 mtg api 发出搜索请求,并且响应是 JSON 格式。我正在寻找显示它们的值
-
哦,我明白了,这是来自外部 API 的 json 响应。不相关,但我会将该代码移出控制器并放入服务类 - 使控制器更易于测试。
-
谢谢,我会的。
标签: html ruby-on-rails ruby ruby-on-rails-5