【问题标题】:Pulling data from a JSON file从 JSON 文件中提取数据
【发布时间】:2017-04-03 09:38:55
【问题描述】:

嘿,我正在尝试在这个 JSON 文件中打印 cmets:

    # https://www.reddit.com/r/android/comments.json?limit=1

    require 'json'

    file = File.read('comments.json')
    data_hash=JSON.parse(file)
    comment = data_hash.fetch("body")
    print comment

当我运行它时,它说找不到正文键?

【问题讨论】:

    标签: json ruby hash


    【解决方案1】:
    require 'json'
    
    file = File.read('comments.json')
    data_hash=JSON.parse(file)
    require 'pp'
    pp data_hash
    

    返回

    {"kind"=>"Listing",
     "data"=>
      {"modhash"=>"",
       "children"=>
        [{"kind"=>"t1",
          "data"=>
           {"subreddit_id"=>"t5_2qlqh",
            "edited"=>false,
            "banned_by"=>nil,
            "removal_reason"=>nil,
            "link_id"=>"t3_5dq9i2",
            "link_author"=>"crazyg0od33",
            "likes"=>nil,
            "replies"=>"",
            "user_reports"=>[],
            "saved"=>false,
            "id"=>"da73zcw",
            "gilded"=>0,
            "archived"=>false,
            "report_reasons"=>nil,
            "author"=>"not_a_miscarriage",
            "parent_id"=>"t1_da73w7s",
            "score"=>1,
            "approved_by"=>nil,
            "over_18"=>false,
            "controversiality"=>0,
            "body"=>"Oh. The more you know",
            "link_title"=>
             "Get the Google Home speaker for $99 at Best Buy on Black Friday",
            "author_flair_css_class"=>nil,
            "downs"=>0,
            "body_html"=>
             "<div class=\"md\"><p>Oh. The more you know</p>\n</div>",
            "quarantine"=>false,
            "subreddit"=>"Android",
            "name"=>"t1_da73zcw",
            "score_hidden"=>true,
            "stickied"=>false,
            "created"=>1479604485.0,
            "author_flair_text"=>nil,
            "link_url"=>"http://blackfriday.bestbuy.com/?category=connected+home2",
            "created_utc"=>1479575685.0,
            "distinguished"=>nil,
            "mod_reports"=>[],
            "num_reports"=>nil,
            "ups"=>1}}],
       "after"=>"t1_da73zcw",
       "before"=>nil}}
    

    所以你正在寻找的身体是:

    data_hash["data"]["children"].first["data"]["body"]
    

    有这么多哈希请求,你可能想写:

    data_hash["data"]["children"].first["data"]["body"] rescue ""
    

    【讨论】:

      【解决方案2】:

      您已经在变量data_hash 中通过以下代码将json 数据作为哈希:

      data_hash=JSON.parse(file)
      

      这只是从这个散列变量中获取适当的数据键的问题。您收到此错误,因为您正在阅读的 json 中的正文不在父级别,您可以通过以下代码获取此错误,这是正文键的确切路径:

      >comment = data_hash.fetch("data").fetch("children")[0].fetch("data").fetch("body")
      
      => "This isn't price match, but price protection via a credit card. They'd issue a check for the difference."
      

      你不需要在这里使用fetch方法,因为data_hash已经是一个哈希,所以你也可以简单地做以下事情:

      > data_hash["data"]["children"][0]["data"]["body"]   
      => "This isn't price match, but price protection via a credit card. They'd issue a check for the difference."
      

      同样可以访问其他数据变量。


      在 Ruby 2.3+ 中,您也可以通过以下方式实现:

      data_hash.dig('data', 'children', 0, 'data', 'body')
      

      【讨论】:

      • data_hash.dig('data', 'children', 0, 'data', 'body') 也可以在 Ruby 2.3+ 中使用。
      猜你喜欢
      • 2019-01-22
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多