[英]How can I access an array inside a hash?
我在數組中有一個哈希:
values = {}
values.merge!(name => {
"requested_amount" => @specific_last_pending_quota.requested_amount,
"granted" => @specific_last_pending_quota.granted,
"pending_final" => pending_final
})
@o_requests[request.receiving_organization][request.program_date][:data] = values
我將其發送到視圖,然后在收到時發送:
= quota[:data].inspect
# {"theme"=>{"requested_amount"=>2, "granted"=>false, "pending_final"=>0}}
我想這樣獲取對象:
= quota[:data]["theme"].inspect
但是我得到這個錯誤
can't convert String into Integer
嘗試將其轉換為適當的哈希,我猜它應該可以正常工作。
values = {}
values.merge!(name => {:requested_amount => @specific_last_pending_quota.requested_amount, :granted => @specific_last_pending_quota.granted, :pending_final => pending_final})
@o_requests[request.receiving_organization][request.program_date][:data] = values
我猜為quota[:data]
可能返回哈希數組
嘗試:
= quota[:data][0]["theme"]
在這里,我嘗試案例以得到相同的錯誤並檢查:
> h[:data]
#=> [{"theme"=>{"requested_amount"=>2, "granted"=>false, "pending_final"=>0}}]
> h[:data]["theme"]
TypeError: no implicit conversion of String into Integer
from (irb):10:in `[]'
from (irb):10
from /home/ggami/.rvm/rubies/ruby-2.2.1/bin/irb:11:in `<main>'
> h[:data][0]["theme"]
#=> {"requested_amount"=>2, "granted"=>false, "pending_final"=>0}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.