iT邦幫忙

0

RUBY取字串中的內容

  • 分享至 

  • twitterImage

我想在聊天機器人中抓字串中的內容,但卻失敗了
我想在這個字串中
{
"query": "吃飯",
"topScoringIntent": {
"intent": "找餐廳",
"score": 0.9424145
},
取出"topScoringIntent" 中的 "intent"內的 "找餐廳"
我用irb測試可以使用hash[:topScoringIntent][:intent]中取出"找餐廳"的字串
但在聊天機器人中卻有錯誤,log顯示no implicit conversion of Symbol into Integer
而且我發現在聊天機器人中hash的一些函式都undefined method....不知道為什麼....

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
Darwin Watterson
iT邦好手 1 級 ‧ 2019-08-31 21:06:55

首先, 你要先知道你處理的是一個 JSON 資料, 而非單純的字串 !

{   "query": "吃飯",
    "topScoringIntent": {
                            "intent": "找餐廳",
                            "score": 0.9424145
                        }
}                        

再來, 你可以在Google找找 Ruby json parsing 這類的關鍵字找找 json 在 Ruby 上的資源 !
我找到的Ruby Json parsing教學(英文版)
目前各式各樣的程式語言都有不只一套可以解析 JSON 結構的套件 !

最後就是理解教學改成你自己要的需求就大功告成了 !
Good Luck ! /images/emoticon/emoticon12.gif

已解決

我要發表回答

立即登入回答