我有一個紅寶石碼塊,如下所示:如何使用ruby來遍歷這個json文檔?
require "elasticsearch"
require "json"
search_term = "big data"
city = "Hong Kong"
client = Elasticsearch::Client.new log: true
r = client.search index: 'candidates', body:
{
query: {
bool: {
must: [
{
match: {
tags: search_term
}
},
{
match: {
city: city
}
}
]
}
}
}
它產生多個返回像這樣的:
{"_index":"candidates","_type":"data",
"_id":"AU3DyAmvtewNSFHuYn88",
"_score":3.889237,
"_source":{"first":"Kota","last":"Okayama","city":"Tokyo","designation":"Systems Engineer","email":"[email protected]","phone":"phone","country":"Japan","industry":"Technology","tags":["remarks","virtualization big data"]}}
我想來遍歷它並提取各種元素。我曾嘗試
data = JSON.parse(r)
data.each do |row|
puts row["_source"]["first"]
end
和錯誤是:
no implicit conversion of Hash into String (TypeError)
什麼是最好的前進道路上這個傢伙?
剛剛嘗試包括完整的字符串爲單引號試試下面的代碼IRB H ='{ 「_指數」: 「考生」, 「_類型」: 「數據」, 「_id」: 「AU3DyAmvtewNSFHuYn88」, 「_score」 :3.889237, 「_source」:{「first」:「Kota」,「last」:「岡山」,「city」:「東京」,「指定」:「系統工程師」,「email」:「user @ hotmail .co.jp「,」phone「:」phone「,」country「:」Japan「,」industry「:」Technology「,」tags「:[」備註「,」虛擬化大數據「]}}' 那麼你可以訪問值h [「國家」] –
謝謝,這將返回多個錯誤。我希望能夠提取元素以將它們打印爲HTML,並使用電子郵件地址發送廣播電子郵件 – user1903663
您可以請檢查一下您是否錯過了某些東西,因爲我剛剛嘗試它後從控制檯上粘貼了它 –