从JSON对象读取值并使用Ruby

时间:2015-11-28 19:28:59

标签: ruby json

我想将基于时间at的响应对象中的值“V”添加到单个对象中。我们将使用单个或多个对象获得不同的响应。例如,解析响应后,以下数据有三个对象:

data = [
  {
    "MeterId"=>"201_12",
    "MeterName"=>"WHr",
    "Values"=> [
      {"at"=>"2015-11-25 01:00:00", "v"=>"90.00"}, 
      {"at"=>"2015-11-25 01:10:00", "v"=>"10.00"}
    ]
  },
  {
    "MeterId"=>202_12",
    "MeterName"=>"WHr",
    "Values"=>[
      {"at"=>"2015-11-25 01:00:00", "v"=>"50.00"}, 
      {"at"=>"2015-11-25 01:10:00", "v"=>"60.00"}
    ]
  },
  {
    "MeterId"=>"203_12",
    "MeterName"=>"WHr",
    "Values"=>[
      {"at"=>"2015-11-25 01:00:00", "v"=>"70.00"}, 
      {"at"=>"2015-11-25 01:10:00", "v"=>"40.00"}
    ]
  }
]

实际输出:

#<DataObject:0x00000002e3fee8@parameter="RefPowerSubscription", 
@samples=[
#<DataHash:0x00000002e3fda8 @value="90.00", @time_stamp="2015-11-25 01=>00=>00", @count=0, @name="">, 
#<DataHash:0x00000002e3fd30 @value="10.00", @time_stamp="2015-11-25 01=>10=>00", @count=0, @name="">, 
#<DataHash:0x00000002e3fc90 @value="50.00", @time_stamp="2015-11-25 01=>00=>00", @count=0, @name="">, 
#<DataHash:0x00000002e3fc18 @value="60.00", @time_stamp="2015-11-25 01=>10=>00", @count=0, @name="">, 
#<DataHash:0x00000002e3fb78 @value="70.00", @time_stamp="2015-11-25 01=>00=>00", @count=0, @name="">, 
#<DataHash:0x00000002e3fb00 @value="40.00", @time_stamp="2015-11-25 01=>10=>00", @count=0, @name="">], 
@max=0, @min=0, @sum=0>

我需要这种格式的输出:

#<DataObject:0x00000002eb85f0 @parameter="RefPowerSubscription", 
@samples=[
#<DataHash:0x00000002eb84b0 @value="210.00", @time_stamp="2015-11-25 01=>00=>00", @count=0, @name="">,
#<DataHash:0x00000002eb8208 @value="110.00", @time_stamp="2015-11-25 01=>10=>00", @count=0, @name="">], 
@max=0, @min=0, @sum=0>

这是我的代码。它正在使用一个对象,但我不知道如何处理多个对象。这里DataObject是类,我们用它来存储哈希值。任何人都可以帮我解决逻辑问题吗?

class DataObject
  attr_accessor :count, :samples, :average, :parameter

  def initialize(parameter,sample_size = 0)
    @parameter = parameter
    @samples = []

    sample_size.times do
      @samples << DataHash.new
    end

    @max = 0
    @min = 0
    @sum = 0
  end
end
class DataHash
  attr_accessor :value, :time_stamp, :count , :name

  def initialize
    @value = 0
    @time_stamp = ''
    @count = 0
    @name = ''
  end
end
dobj = DataObject.new('RefPowerSubscription')
dhash = DataHash.new
len = data.length

until i == len do 
  data[i]['Values'].each do | data |
    dhash.time_stamp = data['at']
    dhash.value = data['v']
    dobj.samples << dhash.dup
  end

  i += 1
end

p dobj

工作代码

vtime = Time.parse(stime).to_s
i =0
loop do
value = 0

data.reduce(len) do | memo, temp |

temp['Values'].reduce(temp.size) do | sum , order|
    if Time.parse(vtime) == Time.parse(order['at'])
        value += order['v'].to_i
    end
end

end
dhash.time_stamp = Time.parse(vtime)  # order['at']
dhash.value = value 
dobj.samples[i] = dhash.clone

vtime = (Time.parse(vtime) + 600 ).to_s
i +=1
break if Time.parse(vtime) > Time.parse(etime)
end

p dobj

1 个答案:

答案 0 :(得分:0)

也许只是把它放在那里:

timestamps_hash = {}

 data.each do |outer|
   outer["Values"].each do |inner|
     if timestamps_hash.key? inner["at"]
       timestamps_hash[inner["at"]] += inner["v"].to_f
     else
       timestamps_hash[inner["at"]] = inner["v"].to_f
     end
   end
 end