###前提・実現したいこと
ruby で json file を開いて必要なデータ(team_code="blue")に絞ってから格納したい。
json
1[ 2 { 3 "date":"20170101", 4 "title":"aiueo", 5 "team_code":"blue", 6 "sub_team_code":"yellow" 7 }, 8 { 9 "date":"20170102", 10 "title":"kakikukeko", 11 "team_code":"yellow", 12 "sub_team_code":"blue" 13 }, 14 { 15 "date":"20170104", 16 "title":"sashisuseso", 17 "team_code":"red", 18 "sub_team_code":"yellow" 19 }, 20 { 21 "date":"20170104", 22 "title":"tatitsuteto", 23 "team_code":"blue", 24 "sub_team_code":"yellow" 25 } 26]
上記をteam_code":"blue"に絞り、
下記のようなjsonを作りたいです。
json
1[ 2 { 3 "date":"20170101", 4 "title":"aiueo", 5 "team_code":"blue", 6 "sub_team_code":"yellow" 7 }, 8 { 9 "date":"20170104", 10 "title":"tatitsuteto", 11 "team_code":"blue", 12 "sub_team_code":"yellow" 13 } 14]
###該当のソースコード
<% require 'json' json_data = File.read('data/sample.json') json_date = **json(team_code="blue"にしぼったもの。** %>
###試したこと①
<% json_data.each do |x| %> <% x["team_code"].include?("blue") %> <%end%>
trueかfalseかまでは返せたのですが、ここから
team_code="blue"のデータだけを取り出せずにいます。
###試したこと②
<% for i in 0..4 do %> <%= articles[i].select{|k, v| v.include?("blue")} %> <% end %>
sub_team_code="blue"も対象となってしまいます。
環境
ruby 2.4.2p198
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/01/23 13:48
2018/01/23 13:52