ruby - 哈希到 JSON,然后在 Ruby 中加入另一个 JSON

标签 ruby json

我有一个这样的 JSON:

[
  {
    "Low": 8.63,
    "Volume": 14211900,
    "Date": "2012-10-26",
    "High": 8.79,
    "Close": 8.65,
    "Adj Close": 8.65,
    "Open": 8.7
  },
  {
    "Low": 8.65,
    "Volume": 12167500,
    "Date": "2012-10-25",
    "High": 8.81,
    "Close": 8.73,
    "Adj Close": 8.73,
    "Open": 8.76
  },
  {
    "Low": 8.68,
    "Volume": 20239700,
    "Date": "2012-10-24",
    "High": 8.92,
    "Close": 8.7,
    "Adj Close": 8.7,
    "Open": 8.85
  }
]

并计算了每天收盘价的简单移动平均线并将其称为变量 sma9day。我想将移动平均值与原始 JSON 结合起来,所以我每天都会得到这样的信息:

  {
   "Low": 8.68,
   "Volume": 20239700,
   "Date": "2012-10-24",
   "High": 8.92,
   "Close": 8.7,
   "Adj Close": 8.7,
   "Open": 8.85,
   "SMA9": 8.92
  }

我用 sma9day 变量做了这个:

h = { "SMA9" => sma9day }
sma9json = h.to_json
puts sma9json

输出这个:

{"SMA9":[8.92,8.93,8.93]}

如何将其放入与 JSON 兼容的格式并将两者连接起来?我需要从上到下“匹配/加入”,因为 JSON 中的最后 8 条记录不会有 9 天移动平均值(在这些情况下,我仍然希望 key 在那里(SMA9),但是值为零或零。

谢谢。

最新更新:

我现在有了这个,这让我非常接近,但是它返回 JSON 中 SMA9 字段中的整个字符串...

require json
require simple_statistics

json = File.read("test.json")
quotes = JSON.parse(json)

# Calculations
def sma9day(quotes, i)
close = quotes.collect {|quote| quote['Close']}
sma9day = close.each_cons(9).collect {|close| close.mean}
end

quotes = quotes.each_with_index do |day, i|
  day['SMA9'] = sma9day(quotes, i)
end

p quotes[0]

 => {"Low"=>8.63, "Volume"=>14211900, "Date"=>"2012-10-26", "High"=>8.79, "Close"=>8.65, "Adj Close"=>8.65, "Open"=>8.7, "SMA9"=>[8.922222222222222, 8.93888888888889, 8.934444444444445, 8.94222222222222, 8.934444444444445, 8.937777777777777, 8.95, 8.936666666666667, 8.924444444444443, 8.906666666666666, 8.912222222222221, 8.936666666666666, 8.946666666666665, 8.977777777777778, 8.95111111111111, 8.92, 8.916666666666666]}

当我尝试在计算结束前做 sma9day.round(2) 时,它给出了一个方法错误(大概是因为数组?),当我做 sma9day[0].round(2) 时,它确实正确舍入,但每条记录当然都具有相同的 SMA。

感谢任何帮助。谢谢

最佳答案

大概是为了在 ruby​​ 中进行计算,你以某种方式解析了 json,并从中得到了一个 ruby​​ 哈希。

为了弄清楚这一点,您有一个 sma9day 值数组和一个对象数组,并且您想要遍历它们。

要做到这一点,像这样的事情应该让你开始:

hashes = JSON.parse( json )
sma9day_values = [9.83, 9.82, etc... ]

hashes.each_with_index do |hash, index|
  if index >= 9
    hash["SMA9"] = sma9day_values[index-9]
  else
    hash["SMA9"] = 0
  end
end

puts hashes.to_json

编辑:

您确实需要尝试入门 ruby​​ 教程。问题是您在数组上调用 round(2) 。 sma9day(quotes, i) 函数中的变量 i 未使用(提示)。也许尝试像 sma9day[i].round(2)

这样的东西

此外,each_with_index 的返回值也不是要赋值的东西。不要那样做,只需在数组上调用 each_with_index 即可。即

quotes = quotes.each_with_index do |day, i| #bad
quotes.each_with_index do |day, i| #good

关于ruby - 哈希到 JSON,然后在 Ruby 中加入另一个 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13134355/

相关文章:

json - 使用 Go 解码 gZip json

ruby-on-rails - accepts_nested_attributes_for 是否也应该正确设置关联?

ruby-on-rails - 如何清除 Rails Action 缓存? (Rails.cache.clear 不工作)

javascript - 我如何在 json stringify 输出中获取特定对象?

arrays - 按键对字典进行排序并将其转换为数组

c++ - 使用 Qt 将\"插入到 jsonb 值中

ruby-on-rails - 为什么我的模型的唯一性验证规范在应该通过时却失败了?

ruby-on-rails - 不匹配的 bundler 版本 - bundler 2、ruby 2.6

ruby-on-rails - Rails 中的 session 到底是什么?从编码的角度来看,它与 Controller 、模型和 View 有何关系?

java - 使用 ModelMapper 将 JsonNode 转换为对象