【问题标题】:Why am I getting "no implicit conversion of String into Integer" when trying to get Nested JSON attribute?为什么在尝试获取嵌套 JSON 属性时出现“没有将字符串隐式转换为整数”?
【发布时间】:2020-07-19 02:44:11
【问题描述】:

我的 Rails 应用程序正在从 Bing API 中读取 JSON,并为每个结果创建一条记录。但是,当我尝试保存其中一个嵌套的 JSON 属性时,我收到了Resource creation error: no implicit conversion of String into Integer

JSON 看起来像这样:

{
  "Demo": {
    "_type": "News",
    "readLink": "https://api.cognitive.microsoft.com/api/v7/news/search?q=european+football",
    "totalEstimatedMatches": 2750000,
    "value": [
      {
        "provider": [
          {
            "_type": "Organization",
            "name": "Tuko on MSN.com"
          }
        ],
        "name": "Hope for football fans as top European club resume training despite coronavirus threat",
        "url": "https://www.msn.com/en-xl/news/other/hope-for-football-fans-as-top-european-club-resume-training-despite-coronavirus-threat/ar-BB12eC6Q",
        "description": "Bayern have returned to training days after leaving camp following the outbreak of coronavirus. The Bundesliga is among top European competitions suspended."
        }
      }

我遇到问题的属性是 [:provider][:name]。

这是我的代码:

  def handle_bing
    @terms = get_terms

    @terms.each do |t|
      news = get_news(t)

      news['value'].each do |n|
        create_resource(n)
      end
    end
  end  

  def get_terms
    term = ["European football"]
  end

  def get_news(term)
    accessKey = "foobar"
    uri  = "https://api.cognitive.microsoft.com"
    path = "/bing/v7.0/news/search"

    uri = URI(uri + path + "?q=" + URI.escape(term))
    request = Net::HTTP::Get.new(uri)
    request['Ocp-Apim-Subscription-Key'] = accessKey

    response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
      http.request(request)
    end

    response.each_header do |key, value|
       # header names are coerced to lowercase
       if key.start_with?("bingapis-") or key.start_with?("x-msedge-") then
          puts key + ": " + value
       end
    end

    return JSON(response.body)
  end


  def create_resource(news)
    Resource.create(
      name: news['name'],
      url: news['url'],
      description: news['description'],
      publisher: news['provider']['name']
    )
  end

我查看了这些问题,但它们对我没有帮助:

Extract specific field from JSON nested hashes

No implicit conversion of String into Integer (TypeError)?

Why do I get "no implicit conversion of String into Integer (TypeError)"?

更新: 我还尝试将代码更新为:

publisher: news['provider'][0]['name'],但我收到了同样的错误。

【问题讨论】:

    标签: ruby-on-rails json ruby


    【解决方案1】:

    因为“provider”是一个数组。

    应该用索引访问它。

    [:value][0][:provider][0][:name]
    

    ”也是如此。

    【讨论】:

    • 对不起,我应该提到我确实根据stackoverflow.com/questions/20790499/… 尝试过。我将其设为“发布者:新闻 ['provider'][0]['name']”,但我得到了同样的错误。我将把它添加到问题中。
    • 发布 [:provider] 时的输出是什么?
    • 如果我这样做 puts news['provider'},我会得到 {"_type"=>"Organization", "name"=>"UPI.com", "image"=>{"thumbnail"=>{ "contentUrl"=>"bing.com/th?id=AR_d88c5b6fb27b317026ee10242444fb8b&pid=news"}}}
    • 当我将publisher: news['provider'][0]['name'] 更改为publisher: news[0]['provider'][0]['name']publisher: [:value][0]['provider'][0]['name'] 时,我得到资源创建错误:nil:NilClass 的未定义方法`[]'。
    • 您需要对所有键使用符号 [:value][0][:provider][0][:name]
    猜你喜欢
    • 2013-02-28
    • 2014-06-06
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    • 2013-11-02
    相关资源
    最近更新 更多