railsで構築中のWEBアプリケーションで、画面に表示する値をネストしているJSONから取得しています。
例えば以下のようなJSON
JSON
1{ 2"filesystem": { 3 "by_device": { 4 "/dev/xvda1": { 5 "kb_size": "6192704", 6 "fs_type": "ext4", 7 "mounts": [ 8 "/" 9 ] 10 }, 11 "tmpfs": { 12 "kb_size": "509300", 13 "fs_type": "tmpfs", 14 "mounts": [ 15 "/dev/shm" 16 ] 17 } 18}
データの取得方法で特定のキーを指定して取得することはできます。
↓これはOKパターン
※実際にはJSONファイルは別のアプリケーションから取得しています。
ruby
1# メソッド 2 def show 3 @node = "json形式のファイル" 4 end 5 6# html側のコード 7<%= @node.filesystem["filesystem2"]["by_device"]["/dev/xvda1"]["fs_type"] %> 8<%= @node.filesystem["filesystem2"]["by_device"]["tmpfs"]["fs_type"] %> 9
しかしながら、
同じ階層のデータの数だけループ処理で表示させたいのですが
<%= @node.filesystem["filesystem2"]["by_device"]["/dev/xvda1"]["fs_type"] %>
<%= @node.filesystem["filesystem2"]["by_device"]["tmpfs"]["fs_type"] %>
**["/dev/xvda1"]と["tmpfs"]**が異なるキーのため以下の用に書き換えたのですがうまく行きませんでした。
何か良い方法はないでしょうか?
目指したこととしては、["by_device"]の"0"番目に入っているキーを取得したいと考えていました。
ruby
1<%= @node.filesystem["filesystem2"]["by_device"][0]["fs_type"] %> 2<%= @node.filesystem["filesystem2"]["by_device"][0]["fs_type"] %>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/14 04:01 編集
2017/09/14 09:19