下面是试图提取数据但不能按预期工作的内容。无法解析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>
不知道这里错过了什么。
'hash'的值是什么? –