2017-09-27 83 views
0

下面是试图提取数据但不能按预期工作的内容。无法解析ruby中的JSON数据

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> 

不知道这里错过了什么。

+0

'hash'的值是什么? –

回答

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的内容序列化以防止此类问题

+0

它的工作。感谢指出的eval,我错过了要记住。 – Karthi1234

-1

试试这个val["local_disk_stats"]["usr"]没有斜杠/

+0

它不会工作,因为密钥名称本身只有'/ usr'。 – Karthi1234

+0

你是对的。 'val.class'返回'hash',但'val [“local_disk_stats”] .class'返回'string'。 btw'val.keys'返回'local_disk_stats'作为散列键。你可以发布你的哈希对象数据吗? –

+0

更新问题本身的数据 – Karthi1234