从 Ruby 中的 JSON 文件解析并从嵌套哈希中提取数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40942569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:44:20  来源:igfitidea点击:

Parsing from a JSON file in Ruby and Extract numbers from Nested Hashes

jsonrubyparsing

提问by Sookie J

Now I am working on extracting information from a JSON file in Ruby. Then how can I extract just the numbers next to the word 'score' from the following text file? For example, I want to get 0.6748984055823062, 0.6280145725181376 on and on.

现在我正在从 Ruby 中的 JSON 文件中提取信息。那么如何从以下文本文件中仅提取单词“分数”旁边的数字?例如,我想得到 0.6748984055823062、0.6280145725181376 等等。

{
  "sentiment_analysis": [
    {
      "positive": [
        {
          "sentiment": "Popular",
          "topic": "games",
          "score": 0.6748984055823062,
          "original_text": "Popular games",
          "original_length": 13,
          "normalized_text": "Popular games",
          "normalized_length": 13,
          "offset": 0
        },
        {
          "sentiment": "engaging",
          "topic": "pop culture-inspired games",
          "score": 0.6280145725181376,
          "original_text": "engaging pop culture-inspired games",
          "original_length": 35,
          "normalized_text": "engaging pop culture-inspired games",
          "normalized_length": 35,
          "offset": 370
        },
     "negative": [
    {
      "sentiment": "get sucked into",
      "topic": "the idea of planning",
      "score": -0.7923352042939829,
      "original_text": "Students get sucked into the idea of planning",
      "original_length": 45,
      "normalized_text": "Students get sucked into the idea of planning",
      "normalized_length": 45,
      "offset": 342
    },
    {
      "sentiment": "be daunted",
      "topic": null,
      "score": -0.5734506634410159,
      "original_text": "initially be daunted",
      "original_length": 20,
      "normalized_text": "initially be daunted",
      "normalized_length": 20,
      "offset": 2104
    },

What I have tried is that I could read a file and set the text file to a hash variable using the JSON method.

我尝试过的是,我可以读取文件并使用 JSON 方法将文本文件设置为哈希变量。

require 'json'
json = JSON.parse(json_string)

采纳答案by Arun Kumar Mohan

You can use Array#mapto collect the reviews.

您可以Array#map用来收集评论。

reviews = json['sentiment_analysis'][0]
positive_reviews = reviews['positive']
negative_reviews = reviews['negative']

positive_reviews.map { |review| review['score'] }
=> [0.6748984055823062, 0.6280145725181376]

negative_reviews.map { |review| review['score'] }
=> [-0.7923352042939829, -0.5734506634410159]

Hope this helps!

希望这可以帮助!

回答by Michal ?tein

Using the JSONclass:

使用JSON类:

Importing a file:

导入文件:

require "json"
file = File.open "/path/to/your/file.json"
data = JSON.load file

Optionally, you can close it now:

或者,您现在可以关闭它:

file.close

The file looks like this:

该文件如下所示:

{
  "title": "Facebook",
  "url": "https://www.facebook.com",
  "posts": [
    "lemon-car",
    "dead-memes"
  ]
}

The file is now able to be read like this:

现在可以像这样读取文件:

data["title"]
=> "Facebook"
data.keys
=> ["title", "url", "posts"]
data['posts']
=> ["lemon-car", "dead-memes"]
data["url"]
=> "https://www.facebook.com"

Hope this helped!

希望这有帮助!

回答by Blair Anderson

Parse Data from File

从文件解析数据

data_hash = JSON.parse(File.read('file-name-to-be-read.json'))

data_hash = JSON.parse(File.read('file-name-to-be-read.json'))

Then just map over the data!

然后只需映射数据!

reviews = data_hash['sentiment_analysis'].first
reviews.map do |sentiment, reviews|
  puts "#{sentiment} #{reviews.map { |review| review['score'] }}"
end

I think this is the simplest answer.

我认为这是最简单的答案。