如何访问字符串中的哈希

时间:2014-02-22 18:26:41

标签: ruby

我正在阅读CSV文件。它包含一列

{"doctype"=>"birthrecord", "records"=>[{"pagenum"=>"5", "recordId"=>"7", "tagGroups"=>[{"data"=>{"first"=>"given_name", "given_name"=>"Severiano ", "surname"=>"Bustamante"}}]}]}

此列的标题是输出。当我按行[“输出”]将其返回的哈希值作为

"{"doctype"=>"birthrecord", "records"=>[{"pagenum"=>"5", "recordId"=>"7", "tagGroups"=>[{"data"=>{"first"=>"given_name", "given_name"=>"Severiano ", "surname"=>"Bustamante"}}]}]}"

如何像普通哈希一样访问哈希?

任何人都可以帮助我。

1 个答案:

答案 0 :(得分:1)

数据集看起来几乎与JSON类似,除了=>代替:,因此替换它们现在可以像JSON对象一样充分地解析它

JSON.parse('{"doctype"=>"birthrecord", "records"=>[{"pagenum"=>"5", "recordId"=>"7", "tagGroups"=>[{"data"=>{"first"=>"given_name", "given_name"=>"Severiano ", "surname"=>"Bustamante"}}]}]}'.gsub("=>", ":"))["records"][0]["tagGroups"][0]["data"]
#=> {"first"=>"given_name", "given_name"=>"Severiano ", "surname"=>"Bustamante"}