Dashing Meter读取文件Json并发送值

时间:2015-07-24 09:34:52

标签: ruby json dashing

我需要从json文件向dash仪表发送一个值: 解析也不工作内容= JSON.parse(file.read)

require 'json'


file = File.open('/mnt/json/process1.json')
contents = file.read

SCHEDULER.every '2s' do


  contents["poolused"] = { label: "poolused" , value: (contents["poolused"][:value])}
  send_event('synergy3',   { items: contents.values})

end

这是我的json文件

{   
    "label":"value",
    "PoolName":  "SDS2_D2600-SAS-450G-15K",
    "poolpercent":  "70",
    "Date":  "07/23/2015 15:33:57",
    "pooltotal":  "3540529446912",
    "poolused":  "2466384969728"
}

错误:

scheduler caught exception:
undefined method `[]' for nil:NilClass
/home/toto/dashboard_v2/jobs/sample.rb:22:in `block in <top (required)>'

3 个答案:

答案 0 :(得分:1)

您的contents变量包含从文件中读取的JSON字符串。 要将它转换为哈希,你需要 contents = JSON.parse(file.read)

答案 1 :(得分:1)

/opt/ruby/lib/ruby/gems/2.2.0/gems/json-1.8.3/lib/json/common.rb:155:in `parse': 757: unexpected token at '▒▒{' (JSON::ParserError)

错误:

file = File.open('/mnt/json/process1.json') 
contents = JSON.parse(file.read)        

答案 2 :(得分:0)

使用powershell导出文件json:&#34; ASCII&#34;不是UTF 16

 | ConvertTo-Json | Out-file d:\products\Dashing\json\process2.json -encoding ASCII

将值发送到破折号

require 'json'


file = File.read('./process3.json')
counts = JSON.parse(file)

SCHEDULER.every '2s' do

  synergy14 = counts["poolused"]

  send_event('synergy3',   { value: synergy14} ) 

end