【发布时间】: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)
【问题讨论】:
-
这基于stackoverflow.com/q/40936239/128421,其中选择的答案解释了如何对数组中的第一个元素执行此操作。基于此,您尝试过什么?您需要弄清楚如何迭代数组。请阅读“How to Ask”,包括链接页面,以及“minimal reproducible example”和meta.stackoverflow.com/questions/261592/…