gpt4 book ai didi

ruby - 无法将 Hash 转换为 String?

转载 作者:数据小太阳 更新时间:2023-10-29 06:43:14 25 4
gpt4 key购买 nike

我正在尝试解析来自 Wordnik 的 API 的 JSON 响应。这是用 Sinatra 构建的。我不断收到错误“/word 的 TypeError”“无法将 Hash 转换为 String”。我是否错误地使用了 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

最佳答案

您可能正在获取哈希值。要转换它,请使用 to_json:

JSON.parse(resp.to_json)

关于ruby - 无法将 Hash 转换为 String?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8500193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com