首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Rails中,没有从JSON输出到整数的隐式字符串转换

在Rails中,没有从JSON输出到整数的隐式字符串转换
EN

Stack Overflow用户
提问于 2013-12-13 04:33:44
回答 1查看 1.6K关注 0票数 0

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

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

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

app/model/word.rb

代码语言:javascript
复制
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输出

代码语言:javascript
复制
[
      {
        "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"
      }
    ]

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

EN

回答 1

Stack Overflow用户

发布于 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”:“摘自美国传统英语词典,第四版”},

代码语言:javascript
复制
      {
        "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

代码语言:javascript
复制
[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."
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20553666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档