我希望能够在循环中解析API响应。
我在控制器方法中有这个:
@payout_batch= PayPal::SDK::REST::Payout.get('xxxxxxx')
logger.info "Got Payout Batch Status[#{@payout_batch.batch_header.payout_batch_id}]"
rescue ResourceNotFound => err
logger.error "Payout Batch not Found"
end
我可以显示这样的结果:
<%= @payout_batch.batch_header.amount.value %>
但我希望能够循环遍历.each循环中的所有内容,如果可行的话......我尝试过几种方法,但似乎没有任何工作:
<% @payout_batch.batch_header.each do |x| %>
<%= (x["amount"]) %>
<% end %>
还有很多类似的方法。尝试用以下方法定义响应:
json = JSON.parse(@payout_batch)
并使用json循环,但这似乎无法正常工作。
问题:如何通过循环在视图中产生响应?
PayPal和Stripe Payment Gateway的响应始终采用数组的形式。 (现在,我不知道其他网关,因为我已经在这两个网站上工作。)
你可以说他们必须维护一个共同的结构来处理单个记录或多个记录,这就是为什么它在每个支付网关的大多数情况下以数组的形式返回。
所以你必须这样做。
<%= @payout_batch.items[0].batch_header %>