gpt4 book ai didi

html - 通过 json 并在 View 中显示数据的正确语法是什么 - Rails 5?

转载 作者:数据小太阳 更新时间:2023-10-29 08:02:38 24 4
gpt4 key购买 nike

我在显示来自 api 的 json 响应中的所有值时遇到了一点问题,尤其是在 rullings 部分,其中一些值在数组中并且其中有数组。

我在显示数据方面取得了一些成功,但在数组方面我遇到了困难
我搜索了许多相关问题,但许多问题似乎都集中在生成应该显示的 json

以下是我到目前为止所做的,希望我能理解:

Controller :

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>&nbsp;</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"
}
]}

最佳答案

I'm having a little trouble displaying all of the values in a json response from an api especially round the rullings part where some of the values are in an array and have arrays in them.

您必须进一步遍历这些数组以获取显示它们的值

rulings: <br>
<% json['rulings'].each do |ruling| %><br>
<% end %>
<% json['legalities'].each do |play|%>
legality <%= play['format'] %> <%= play['legality'] %><br>
<% end %>

关于html - 通过 json 并在 View 中显示数据的正确语法是什么 - Rails 5?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44614302/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com