【问题标题】:Parsing from a JSON file in Ruby and Extract numbers from Nested Hashes从 Ruby 中的 JSON 文件解析并从嵌套哈希中提取数字
【发布时间】:2017-04-17 23:25:10
【问题描述】:

现在我正致力于从 Ruby 中的 JSON 文件中提取信息。那么如何从以下文本文件中仅提取单词“score”旁边的数字?例如,我想得到 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
    },

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

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

【问题讨论】:

标签: json ruby parsing


【解决方案1】:

使用JSON 类:

导入文件:

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

您也可以现在关闭它:

file.close

文件如下所示:

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

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

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

希望这有帮助!

【讨论】:

  • 您不应该使用File.read,它将整个文件加载到内存中。 File.openJSON.load 配合得很好。
  • 如果这是您选择的选项,请不要忘记file.close
  • 你需要JSON.load而不是JSON.parse,解析只需要一个字符串,加载将需要一个文件。您还需要 require "json" 而不是 include。
【解决方案2】:

从文件中解析数据:

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

然后只需映射数据!

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

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

【讨论】:

  • 是的,但是除了在 IRB 中尝试之外,您可能不知道如何阅读它
  • @MichalŠtein - 这个答案似乎很简单,并且处理关闭文件(你的答案没有) - 请解释你对这个答案的担忧?
  • 答案并没有说明加载后要做什么。我要添加文件关闭,这是个好主意。
【解决方案3】:

您可以使用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]

希望这会有所帮助!

【讨论】:

  • 按照您的建议,它非常有效。谢谢!你能解释一下 json['sentiment_analysis'][0]['positive'] 中的 [0] 是什么意思吗?
  • json['sentiment_analysis'] 包含一个对象数组,您要查找的数据存在于第一个索引中。因此,您使用 [0] 对对象进行索引,然后解析 "positives" 对象。这有意义吗?
  • 好的,我明白了。那你能再看看上面的文本文件吗?我在文本中添加了更多“否定”。在这种情况下,我应该使用 json['sentiment_analysis'][0]['negative'] 还是 json['sentiment_analysis'][1]['negative']?
  • @SookieJ 更新了答案。看看,如果有帮助,请告诉我。
猜你喜欢
  • 1970-01-01
  • 2020-04-22
  • 1970-01-01
  • 1970-01-01
  • 2018-04-24
  • 1970-01-01
  • 1970-01-01
  • 2016-03-02
  • 1970-01-01
相关资源
最近更新 更多