【问题标题】:Hash to JSON then joining with another JSON in Ruby哈希到 JSON,然后在 Ruby 中加入另一个 JSON
【发布时间】:2012-10-31 18:10:48
【问题描述】:

我有一个这样的 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 天移动平均值(在这些情况下,我仍然希望密钥在那里(SMA9),但是有 nil 或 0 作为值。

谢谢。

最新更新:

我现在有了这个,这让我非常接近,但是它返回 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


    【解决方案1】:

    大概是为了在 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
    

    【讨论】:

    • 嗨 AJ,我想我对此发表了评论,抱歉没有尽快回复。我昨天玩了你的代码,但无法让它为我工作。然后托马斯发布了,我一直在尝试解决这个问题。感谢您的帮助(再次)!
    • 嗨 AJ - 很抱歉回复晚了 - 正如你所建议的,我做了一些(更多)红宝石教程,我很高兴地说一切正常!我一直遇到舍入问题,然后意识到这是因为 json 末尾的 nil 值(试图舍入 nil)。所以我添加了一个 if 语句,仅当值不为零时才舍入。再次感谢大家的帮助!
    【解决方案2】:

    我听取了您的意见并在this gist 中编译了一个解决方案。希望对你有帮助。

    【讨论】:

    • 非常感谢托马斯。我有一个小问题,因为我计算的平均值有点不同,你介意看看上面的更新 #2 并告诉我你的想法吗?再次感谢你的帮助。 PS:我注意到你做了一个 sma9.round(3) 来得到一个包含三个整数的数字?有没有办法强制两位小数(如果我不知道小数点前有多少位,但想确保有两位小数?)。
    • simple_statistics gem 的作用如下:source code。还有许多其他方法可以计算未加权平均值,结果相同,其中一种显示在要点中。
    • 是的,但我也使用加权平均值,因此使用 gem 是最简单的。不过感谢您的帮助
    猜你喜欢
    • 2012-02-21
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 2012-07-08
    相关资源
    最近更新 更多