2012-05-29 103 views
0

我有這種格式的數據:Ruby - 如何從具有其他數組和哈希的數組中獲取值?

{"values"=> 
[ 
{"updateKey"=>"UNIU-16268324-5608633092144111616-SHARE", 
"updateComments"=>{"_start"=>1, 
"values"=>[{"comment"=>"Sample test this is mike testing.", 
    "person"=>{"siteStandardProfileRequest"=>{"url"=>"http://www.linkedin.com/profile?viewProfile=&key=116654056&authToken=xVLh&authType=name&trk=api*a140290*s148640*"}, 
    "lastName"=>"DeLorenzo", 
    "headline"=>"--", 
    "id"=>"QM86-RIKjb", 
    "pictureUrl"=>"http://media.linkedin.com/mpr/mprx/0_OurBLnHMma92b4U4pecXLq2MuW8aFOo4ywXHLqEbPuNO4sjZtEhUwNOWCFhAkJIq07-WoP4V_Zqg", "firstName"=>"Jennifer", 
    "apiStandardProfileRequest"=>{"url"=>"http://api.linkedin.com/v1/people/QM86-RIKjb",  "headers"=>{"values"=> 
[{"name"=>"x-li-auth-token", 
"value"=>"name:xVLh"}], 
"_total"=>1}}}, 
"timestamp"=>1337202768000, 
"sequenceNumber"=>1, 
"id"=>80942976}, 
{"comment"=>"123", 
"person"=>{"siteStandardProfileRequest"=>{"url"=>"http://www.linkedin.com/profile?viewProfile=&key=70292133&authToken=wEAo&authType=name&trk=api*a140290*s148640*"}, "lastName"=>"Kartsovnyk", "headline"=>"Snr Software Developer", "id"=>"7-32mNn-Ob", "pictureUrl"=>"http://media.linkedin.com/mpr/mprx/0_xjj5WPXU6fCjCExLjUa9WA69XWNS 

我所試圖做的是通過updateComments循環,並獲得用戶的每個「id」屬性。

這是我到目前爲止有:

if linked_in_updates.values.updateComments.is_a?(Hash) 
    linked_in_updates.each { |key, value| 
    some_value = key 
    } 
end 

我試圖提取哈希是與此updateComments:linked_in_updates.values.updateComments,但我得到的錯誤是這樣的:

NoMethodError: undefined method `updateComments' for #<Array:0x13228fc20> 

任何想法我做錯了什麼?

+1

那[看起來像JSON(http://developer.yahoo.com/ruby/ruby-json.html)與文字上的'\ n'而不是linebreaks –

+0

@Michael會對我如何提取值產生影響? – GeekedOut

回答

1

嘗試更換

linked_in_updates.values.updateComments.is_a?(Hash) 

linked_in_updates.values[0]['updateComments'].is_a?(Hash) 
+0

謝謝!你可以請exaplain給我爲什麼語法是值[0] ['updateComments']?我不確定這是一個二維數組。當我允許時,我會接受答案。 – GeekedOut

+0

順便說一句,我也得到了錯誤:TypeError:我嘗試了你的建議後,不能將字符串轉換爲整數。 – GeekedOut

+0

在'if'linked_in_updates.values.updateComments.is_a(哈希)'之前加上'Rails.logger.info linked_in_updates.values [0]''然後運行'tail -f log/development.log' – alexkv

相關問題