我正在努力呈现基于 JSON 的 API 的结果,并且正在努力解决如何正确迭代结果。我的 API 调用的要点:
@invoice = ActiveSupport::JSON.decode(api_response.to_json)
生成的哈希数组如下:
{
"amount_due"=>4900, "attempt_count"=>0, "attempted"=>true, "closed"=>true,
"currency"=>"usd", "date"=>1350514040, "discount"=>nil, "ending_balance"=>0, "livemode"=>false,
"next_payment_attempt"=>nil, "object"=>"invoice", "paid"=>true, "period_end"=>1350514040, "period_start"=>1350514040, "starting_balance"=>0,
"subtotal"=>4900, "total"=>4900,
"lines"=>{
"invoiceitems"=>[],
"prorations"=>[],
"subscriptions"=>[
{"quantity"=>1,
"period"=>{"end"=>1353192440, "start"=>1350514040},
"plan"=>{"id"=>"2", "interval"=>"month", "trial_period_days"=>nil, "currency"=>"usd", "amount"=>4900, "name"=>"Basic"},
"amount"=>4900}
]
}}
我正在尝试遍历并显示所有“行”以进行渲染和开票。每个“行”可以有 0 个或多个“invoiceitems”、“prorations”和“subscriptions”。
我已经做到了这一点,但无法弄清楚我们如何处理任何嵌套。
<% @invoice["lines"].each_with_index do |line, index| %>
# not sure what the syntax is here ?
<% end %>
我目前正在视图中工作,但是一旦我对其进行排序,就会将其中的大部分内容移至帮助程序。
谢谢!