Ruby on rails Rails中JSON输出没有将字符串隐式转换为整数

Ruby on rails Rails中JSON输出没有将字符串隐式转换为整数,ruby-on-rails,ruby,json,Ruby On Rails,Ruby,Json,首先,我要说我对Ruby和JSON非常陌生 在我的模型文件中,我试图将JSON解析为散列,然后将特定的keys值返回给视图 我当前收到错误消息“没有将字符串隐式转换为整数”。我的理解是JSON输出返回一个散列数组,因此我尝试访问并返回第一个数组键值 app/models/word.rb def self.search(search) result_json = Wordnik.word.get_definitions(search) result_hash = JSON.pars

首先,我要说我对Ruby和JSON非常陌生

在我的模型文件中,我试图将JSON解析为散列,然后将特定的keys值返回给视图

我当前收到错误消息“没有将字符串隐式转换为整数”。我的理解是JSON输出返回一个散列数组,因此我尝试访问并返回第一个数组键值

app/models/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_定义的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"
      }
    ]

我收到错误消息“没有将字符串隐式转换为整数”

首先,我认为您的JSON存在问题,JSON应该使用
,而不是
=>
,然后我将JSON负载重新格式化为

[ { “textProns”:[], “sourceDictionary”:“ahd遗产”, “示例”:[], “相关词语”:[], “标签”:[], “引文”:[], “词”:“谬误”, “文本”:“一个错误的概念。”, “序列”:“0”, “分数”:0.0, “词类”:“名词”, “属性文本”:“摘自美国传统®英语词典,第四版” },

      {
        "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

[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."

将此数据复制到IRB中,然后按照您的代码从
result\u hash=JSON.parse…
开始,我得到了一个错误的概念因此。您是否省略了此处的任何步骤?您上面发布的数据实际上是一个哈希数组。当使用我的模型文件中的以下代码创建新Word对象时,该输出来自控制台:“def initialize(name)”self.name=name“”end“”def def definitions“”Wordnik.Word.get_definitions(self.name)“'end'但是,Wordnik API文档指定输出数据的默认格式为JSON,因此我不确定self.search方法的第一行中数据输出的形式是什么。。。