无法解析ruby中的JSON数据

时间:2017-09-27 10:28:45

标签: json ruby

以下是尝试提取数据但未按预期工作的内容。

irb(main):004:0> config = YAML.load_file("/etc/test.yaml")
.
.
.
irb(main):005:0* asn = config["local_disk_stats"]
=> "{\"/usr\"=> {\"Used\"=> \"4263MB\", \"Total\"=> \"5034MB\", \"Free\"=> \"514MB\"}, \"/var\"=> {\"Used\"=> \"883MB\", \"Total\"=> \"3018MB\", \"Free\"=> \"1981MB\"}, \"/\"=> {\"Used\"=> \"1484MB\", \"Total\"=> \"2015MB\", \"Free\"=> \"428MB\"}, \"/home\"=> {\"Used\"=> \"96MB\", \"Total\"=> \"1019MB\", \"Free\"=> \"870MB\"}}"
irb(main):007:0> hash = Hash.new
=> {}
irb(main):008:0> hash["local_disk_stats"] = asn
=> "{\"/usr\"=> {\"Used\"=> \"4263MB\", \"Total\"=> \"5034MB\", \"Free\"=> \"514MB\"}, \"/var\"=> {\"Used\"=> \"883MB\", \"Total\"=> \"3018MB\", \"Free\"=> \"1981MB\"}, \"/\"=> {\"Used\"=> \"1484MB\", \"Total\"=> \"2015MB\", \"Free\"=> \"428MB\"}, \"/home\"=> {\"Used\"=> \"96MB\", \"Total\"=> \"1019MB\", \"Free\"=> \"870MB\"}}"    
irb(main):041:0> string = JSON.pretty_generate(hash)
=> "{\n  \"local_disk_stats\": \"{\\\"/usr\\\"=> {\\\"Used\\\"=> \\\"4263MB\\\", \\\"Total\\\"=> \\\"5034MB\\\", \\\"Free\\\"=> \\\"514MB\\\"}, \\\"/var\\\"=> {\\\"Used\\\"=> \\\"883MB\\\", \\\"Total\\\"=> \\\"3018MB\\\", \\\"Free\\\"=> \\\"1981MB\\\"}, \\\"/\\\"=> {\\\"Used\\\"=> \\\"1484MB\\\", \\\"Total\\\"=> \\\"2015MB\\\", \\\"Free\\\"=> \\\"428MB\\\"}, \\\"/home\\\"=> {\\\"Used\\\"=> \\\"96MB\\\", \\\"Total\\\"=> \\\"1019MB\\\", \\\"Free\\\"=> \\\"870MB\\\"}}\"\n}"
irb(main):042:0> 
irb(main):044:0> val = JSON.parse(string)
=> {"local_disk_stats"=>"{\"/usr\"=> {\"Used\"=> \"4263MB\", \"Total\"=>
\"5034MB\", \"Free\"=> \"514MB\"}, \"/var\"=> {\"Used\"=> \"883MB\", \"Total\"=> \"3018MB\", \"Free\"=> \"1981MB\"}, \"/\"=> {\"Used\"=> \"1484MB\", \"Total\"=> \"2015MB\", \"Free\"=> \"428MB\"}, \"/home\"=> {\"Used\"=> \"96MB\", \"Total\"=> \"1019MB\", \"Free\"=> \"870MB\"}}"}
irb(main):045:0> val["local_disk_stats"]
=> "{\"/usr\"=> {\"Used\"=> \"4263MB\", \"Total\"=> \"5034MB\", \"Free\"=> \"514MB\"}, \"/var\"=> {\"Used\"=> \"883MB\", \"Total\"=> \"3018MB\", \"Free\"=> \"1981MB\"}, \"/\"=> {\"Used\"=> \"1484MB\", \"Total\"=> \"2015MB\", \"Free\"=> \"428MB\"}, \"/home\"=> {\"Used\"=> \"96MB\", \"Total\"=> \"1019MB\", \"Free\"=> \"870MB\"}}
irb(main):047:0> val["local_disk_stats"]["/usr"]
=> "/usr"
irb(main):048:0>

不确定这里缺少什么。

2 个答案:

答案 0 :(得分:1)

我想说主要问题是,在config["local_disk_stats"]中,字符串不是有效的JSON,而是Hash实例.to_s

在这种特定情况下,您应该首先解析字符串(请参阅下面的2.4.0 :006)然后再解析eval(val["local_disk_stats"])而不是解析它:

2.4.0 :001 > require 'json'
 => true 
2.4.0 :002 > asn ="{\"/usr\"=> {\"Used\"=> \"4263MB\", \"Total\"=> \"5034MB\", \"Free\"=> \"514MB\"}, \"/var\"=> {\"Used\"=> \"883MB\", \"Total\"=> \"3018MB\", \"Free\"=> \"1981MB\"}, \"/\"=> {\"Used\"=> \"1484MB\", \"Total\"=> \"2015MB\", \"Free\"=> \"428MB\"}, \"/home\"=> {\"Used\"=> \"96MB\", \"Total\"=> \"1019MB\", \"Free\"=> \"870MB\"}}"
 => "{\"/usr\"=> {\"Used\"=> \"4263MB\", \"Total\"=> \"5034MB\", \"Free\"=> \"514MB\"}, \"/var\"=> {\"Used\"=> \"883MB\", \"Total\"=> \"3018MB\", \"Free\"=> \"1981MB\"}, \"/\"=> {\"Used\"=> \"1484MB\", \"Total\"=> \"2015MB\", \"Free\"=> \"428MB\"}, \"/home\"=> {\"Used\"=> \"96MB\", \"Total\"=> \"1019MB\", \"Free\"=> \"870MB\"}}" 
2.4.0 :003 > hash = Hash.new
 => {} 
2.4.0 :004 > hash["local_disk_stats"] = asn
 => "{\"/usr\"=> {\"Used\"=> \"4263MB\", \"Total\"=> \"5034MB\", \"Free\"=> \"514MB\"}, \"/var\"=> {\"Used\"=> \"883MB\", \"Total\"=> \"3018MB\", \"Free\"=> \"1981MB\"}, \"/\"=> {\"Used\"=> \"1484MB\", \"Total\"=> \"2015MB\", \"Free\"=> \"428MB\"}, \"/home\"=> {\"Used\"=> \"96MB\", \"Total\"=> \"1019MB\", \"Free\"=> \"870MB\"}}" 
2.4.0 :005 > string = JSON.pretty_generate(hash)
 => "{\n  \"local_disk_stats\": \"{\\\"/usr\\\"=> {\\\"Used\\\"=> \\\"4263MB\\\", \\\"Total\\\"=> \\\"5034MB\\\", \\\"Free\\\"=> \\\"514MB\\\"}, \\\"/var\\\"=> {\\\"Used\\\"=> \\\"883MB\\\", \\\"Total\\\"=> \\\"3018MB\\\", \\\"Free\\\"=> \\\"1981MB\\\"}, \\\"/\\\"=> {\\\"Used\\\"=> \\\"1484MB\\\", \\\"Total\\\"=> \\\"2015MB\\\", \\\"Free\\\"=> \\\"428MB\\\"}, \\\"/home\\\"=> {\\\"Used\\\"=> \\\"96MB\\\", \\\"Total\\\"=> \\\"1019MB\\\", \\\"Free\\\"=> \\\"870MB\\\"}}\"\n}" 
2.4.0 :006 > val = JSON.parse(string)
 => {"local_disk_stats"=>"{\"/usr\"=> {\"Used\"=> \"4263MB\", \"Total\"=> \"5034MB\", \"Free\"=> \"514MB\"}, \"/var\"=> {\"Used\"=> \"883MB\", \"Total\"=> \"3018MB\", \"Free\"=> \"1981MB\"}, \"/\"=> {\"Used\"=> \"1484MB\", \"Total\"=> \"2015MB\", \"Free\"=> \"428MB\"}, \"/home\"=> {\"Used\"=> \"96MB\", \"Total\"=> \"1019MB\", \"Free\"=> \"870MB\"}}"} 
2.4.0 :007 > val["local_disk_stats"]
 => "{\"/usr\"=> {\"Used\"=> \"4263MB\", \"Total\"=> \"5034MB\", \"Free\"=> \"514MB\"}, \"/var\"=> {\"Used\"=> \"883MB\", \"Total\"=> \"3018MB\", \"Free\"=> \"1981MB\"}, \"/\"=> {\"Used\"=> \"1484MB\", \"Total\"=> \"2015MB\", \"Free\"=> \"428MB\"}, \"/home\"=> {\"Used\"=> \"96MB\", \"Total\"=> \"1019MB\", \"Free\"=> \"870MB\"}}" 
2.4.0 :008 > output = eval(val["local_disk_stats"])
 => {"/usr"=>{"Used"=>"4263MB", "Total"=>"5034MB", "Free"=>"514MB"}, "/var"=>{"Used"=>"883MB", "Total"=>"3018MB", "Free"=>"1981MB"}, "/"=>{"Used"=>"1484MB", "Total"=>"2015MB", "Free"=>"428MB"}, "/home"=>{"Used"=>"96MB", "Total"=>"1019MB", "Free"=>"870MB"}} 
2.4.0 :009 > output['/usr']
 => {"Used"=>"4263MB", "Total"=>"5034MB", "Free"=>"514MB"} 
2.4.0 :010 > output['/usr'].class
 => Hash 

无论如何,我建议您序列化原始YAML文件中local_disk_stats的内容以防止此类问题

答案 1 :(得分:-1)

尝试使用此val["local_disk_stats"]["usr"]而不使用斜杠/