无法将哈希转换为字符串?

585*_*nor 11 ruby

我试图从Wordnik的API解析JSON响应.这是与Sinatra建立的.我不断收到错误"TypeError at/word""无法将Hash转换为字符串".我是否错误地使用了json解析器?

这是我的代码:

get '/word' do
  resp = Wordnik.words.get_random_word(:hasDictionaryDef => 'true', :maxCorpusCount => 20, :minLength => 10)
  result = JSON.parse(resp)
  word = result.word
  return word.to_s
end
Run Code Online (Sandbox Code Playgroud)

NAR*_*KOZ 21

你可能得到一个哈希.要转换它使用to_json:

JSON.parse(resp.to_json)
Run Code Online (Sandbox Code Playgroud)