What is the correct syntax for json traversal and displaying data in a view - Rails 5?

I have a little problem displaying all values โ€‹โ€‹in the json response from api, especially around the rullings part where some of the values โ€‹โ€‹are in an array and there are arrays in them.

p>

I've had some success displaying data, but I'm struggling when it comes to arrays
I've searched many related questions, but many seem to focus on generating the json as supposed to display it.

Below is what I have done so far, and hopefully I make sense:

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

      

View

<% @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 Response:

{"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"
  }
]}

      

+3


source to share


1 answer


I have a little problem with displaying all values โ€‹โ€‹in json response from api, especially around the part of the tubes where some of the values โ€‹โ€‹are in an array and have arrays in them.

You need to continue iterating over these arrays to get the values โ€‹โ€‹to display them.



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

      

+2


source







All Articles