首先我要说的是,我对Ruby和JSON非常陌生。
在我的模型文件中,我尝试将JSON解析为一个散列,然后将一个特定的键值返回给视图。
我现在收到错误信息“没有隐式的字符串转换为整数”。我的理解是,JSON输出返回一个哈希数组,因此我尝试访问并返回第一个数组键值。
app/model/word.rb
def self.search(search)
result_json = Wordnik.word.get_definitions(search)
result_hash = JSON.parse(result_json.to_json) # Parse JSON to hash
definition = result_hash.first["text"] # return first :text key of the hash
end Wordik.word.get_definitions的JSON输出
[
{
"textProns"=>[],
"sourceDictionary"=>"ahd-legacy",
"exampleUses"=>[],
"relatedWords"=>[],
"labels"=>[],
"citations"=>[],
"word"=>"fallacy",
"text"=>"A false notion.",
"sequence"=>"0",
"score"=>0.0,
"partOfSpeech"=>"noun",
"attributionText"=>"from The American Heritage® Dictionary of the English Language, 4th Edition"
},
{
"textProns"=>[],
"sourceDictionary"=>"ahd-legacy",
"exampleUses"=>[],
"relatedWords"=>[],
"labels"=>[],
"citations"=>[],
"word"=>"fallacy",
"text"=>"A statement or an argument based on a false or invalid inference.",
"sequence"=>"1",
"score"=>0.0,
"partOfSpeech"=>"noun",
"attributionText"=>"from The American Heritage® Dictionary of the English Language, 4th Edition"
},
{
"textProns"=>[],
"sourceDictionary"=>"ahd-legacy",
"exampleUses"=>[],
"relatedWords"=>[],
"labels"=>[],
"citations"=>[],
"word"=>"fallacy",
"text"=>"Incorrectness of reasoning or belief; erroneousness.",
"sequence"=>"2",
"score"=>0.0,
"partOfSpeech"=>"noun",
"attributionText"=>"from The American Heritage® Dictionary of the English Language, 4th Edition"
},
{
"textProns"=>[],
"sourceDictionary"=>"ahd-legacy",
"exampleUses"=>[],
"relatedWords"=>[],
"labels"=>[],
"citations"=>[],
"word"=>"fallacy",
"text"=>"The quality of being deceptive.",
"sequence"=>"3",
"score"=>0.0,
"partOfSpeech"=>"noun",
"attributionText"=>"from The American Heritage® Dictionary of the English Language, 4th Edition"
}
]我收到错误消息“没有隐式的字符串转换为整数”。
发布于 2013-12-13 04:48:23
首先,我认为您的JSON有问题,JSON应该使用:而不是=>,然后我将JSON有效负载重新格式化为
[{ "textProns":[],"sourceDictionary":"ahd-legacy","exampleUses":[],"relatedWords":[],"labels":[],"citations":[],“word”:“谬误”,“text”:“错误的概念。”,"sequence":"0","score":0.0,“partOfSpeech”:“名词”,“attributionText”:“摘自美国传统英语词典,第四版”},
{
"textProns":[],
"sourceDictionary":"ahd-legacy",
"exampleUses":[],
"relatedWords":[],
"labels":[],
"citations":[],
"word":"fallacy",
"text":"A statement or an argument based on a false or invalid inference.",
"sequence":"1",
"score":0.0,
"partOfSpeech":"noun",
"attributionText":"from The American Heritage® Dictionary of the English Language, 4th Edition"
},
{
"textProns":[],
"sourceDictionary":"ahd-legacy",
"exampleUses":[],
"relatedWords":[],
"labels":[],
"citations":[],
"word":"fallacy",
"text":"Incorrectness of reasoning or belief; erroneousness.",
"sequence":"2",
"score":0.0,
"partOfSpeech":"noun",
"attributionText":"from The American Heritage® Dictionary of the English Language, 4th Edition"
},
{
"textProns":[],
"sourceDictionary":"ahd-legacy",
"exampleUses":[],
"relatedWords":[],
"labels":[],
"citations":[],
"word":"fallacy",
"text":"The quality of being deceptive.",
"sequence":"3",
"score":0.0,
"partOfSpeech":"noun",
"attributionText":"from The American Heritage® Dictionary of the English Language, 4th Edition"
}
]代码中,我将有效负载复制到一个文件中进行解析,如果有效负载是JSON,我认为您不再需要to_json
[2] pry(main)> a = JSON.parse(File.read("result.json"))
=> [{"textProns"=>[],
"sourceDictionary"=>"ahd-legacy",
"exampleUses"=>[],
"relatedWords"=>[],
"labels"=>[],
"citations"=>[],
"word"=>"fallacy",
"text"=>"A false notion.",
"sequence"=>"0",
"score"=>0.0,
"partOfSpeech"=>"noun",
"attributionText"=>
"from The American Heritage® Dictionary of the English Language, 4th Edition"},
{"textProns"=>[],
"sourceDictionary"=>"ahd-legacy",
"exampleUses"=>[],
"relatedWords"=>[],
"labels"=>[],
"citations"=>[],
"word"=>"fallacy",
"text"=>"A statement or an argument based on a false or invalid inference.",
"sequence"=>"1",
"score"=>0.0,
"partOfSpeech"=>"noun",
"attributionText"=>
"from The American Heritage® Dictionary of the English Language, 4th Edition"},
{"textProns"=>[],
"sourceDictionary"=>"ahd-legacy",
"exampleUses"=>[],
"relatedWords"=>[],
"labels"=>[],
"citations"=>[],
"word"=>"fallacy",
"text"=>"Incorrectness of reasoning or belief; erroneousness.",
[3] pry(main)> a.first
=> {"textProns"=>[],
"sourceDictionary"=>"ahd-legacy",
"exampleUses"=>[],
"relatedWords"=>[],
"labels"=>[],
"citations"=>[],
"word"=>"fallacy",
"text"=>"A false notion.",
"sequence"=>"0",
"score"=>0.0,
"partOfSpeech"=>"noun",
"attributionText"=>
"from The American Heritage® Dictionary of the English Language, 4th Edition"}
[4] pry(main)> a.first["text"]
=> "A false notion."https://stackoverflow.com/questions/20553666
复制相似问题