【问题标题】:Dashing Meter read file Json and send valueDashing Meter 读取文件 Json 并发送值
【发布时间】:2015-07-27 21:56:48
【问题描述】:

我需要从 json 文件向 dashing Meter 发送一个值: 解析也不起作用 content = 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)>'

【问题讨论】:

    标签: ruby json dashing


    【解决方案1】:

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

    【讨论】:

    • 错误 /opt/ruby/lib/ruby/gems/2.2.0/gems/json-1.8.3/lib/json/common.rb:155:in `parse': 757: 意外'▒▒{'处的令牌 (JSON::ParserError)
    • 正如您从错误消息中看到的,当您从文件中读取 JSON 字符串时,JSON '▒▒{' 前面有一些东西。如果您能以某种方式共享该 JSON 文件,我将能够理解问题所在。
    • 感谢 Glupo,但我无法共享文件,一切都被我的业务阻塞了,文件就像顶部的 process1.json
    • 这是我这边的工作方式:pastebin.com/BkUixYeR 将上述 json 内容复制到文件中。您可能需要做的是验证文件中的内容并 gsub 不需要的字符。
    • 我将“-encoding ASCII”放在我的 powershell 脚本中。及其运行。因为它是 UTF -16 并且不起作用。 " | ConvertTo-Json | 输出文件 d:\process2.json -encoding ASCII "
    【解决方案2】:
    /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)        
    

    【讨论】:

      【解决方案3】:

      使用 powershell 导出文件 json:“ASCII”而不是 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-31
        • 1970-01-01
        • 2022-11-27
        相关资源
        最近更新 更多