首先的訪問數據,我在這裏使用這個的Gemfile:嵌套哈希
https://github.com/ryanwkan/covetous
初始化
my_profile後=覬覦::檔案:: Career.new「RWK#1242 「
我得到這樣
012數組當我試圖訪問它像這樣
my_profile.heroes,它返回
[{"name"=>"Ziyi", "id"=>10692899, "level"=>60, "hardcore"=>false, "paragonLevel"=>87, "gender"=>1, "dead"=>false, "class"=>"wizard", "last-updated"=>1354337248}, {"name"=>"Aerendil", "id"=>5987778, "level"=>60, "hardcore"=>false, "paragonLevel"=>0, "gender"=>1, "dead"=>false, "class"=>"demon-hunter", "last-updated"=>1353389408}, {"name"=>"Bubba", "id"=>9177617, "level"=>60, "hardcore"=>false, "paragonLevel"=>11, "gender"=>0, "dead"=>false, "class"=>"witch-doctor", "last-updated"=>1352946041}, {"name"=>"Emma", "id"=>7153459, "level"=>60, "hardcore"=>false, "paragonLevel"=>0, "gender"=>1, "dead"=>false, "class"=>"monk", "last-updated"=>1347863170}, {"name"=>"Grumbar", "id"=>17793743, "level"=>60, "hardcore"=>false, "paragonLevel"=>0, "gender"=>0, "dead"=>false, "class"=>"barbarian", "last-updated"=>1352944313}, {"name"=>"BankerOne", "id"=>12215739, "level"=>1, "hardcore"=>false, "paragonLevel"=>0, "gender"=>1, "dead"=>false, "class"=>"monk", "last-updated"=>1351810350}]
,如果我先走1步,並使用my_profile.heroes [0]它返回
{"name"=>"Ziyi", "id"=>10692899, "level"=>60, "hardcore"=>false, "paragonLevel"=>87, "gender"=>1, "dead"=>false, "class"=>"wizard", "last-updated"=>1354337248}
但是,我無法從上述結果中獲取更多數據。
my_profile.heroes [0]。名稱返回NoMethodError和my_profile.heroes [0] [0]返回 「無」
我是不是做錯了?
在此先感謝
瑞安
這是一個散列,你嘗試使用散列語法嗎? –
@Dave --Hash = my_profile,然後hash [:heroes]返回NoMethodError –
...'my_profile.heroes [0] ['name']',不確定你想用'hash [:heroes] '。 –