如何将 ruby 格式的 json 字符串转换为 ruby 中的 json 哈希? [英] How to convert ruby formatted json string to json hash in ruby?
本文介绍了如何将 ruby 格式的 json 字符串转换为 ruby 中的 json 哈希?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想像哈希对象一样访问 json 字符串,以便我可以使用像 temp["anykey"]
这样的键值访问 json.如何将 ruby 格式的 json 字符串转换为 json 对象?
I want to access json string like hash object so that i can access json using key value like temp["anykey"]
. How to convert ruby formatted json string into json object?
我有以下json字符串
I have following json string
temp = '{"accept"=>"*/*", "host"=>"localhost:4567", "version"=>"HTTP/1.1",
"user_agent"=>"curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3",
"http_token"=>"375fe428b1d32787864264b830c54b97"}'
推荐答案
您可以在 temp json string 上尝试 eval 方法
You can try eval method on temp json string
示例:
eval(temp)
这将返回以下哈希
{"accept"=>"*/*", "host"=>"localhost:4567", "version"=>"HTTP/1.1", "user_agent"=>"curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3", "http_token"=>"375fe428b1d32787864264b830c54b97"}
希望这会有所帮助.
谢谢
这篇关于如何将 ruby 格式的 json 字符串转换为 ruby 中的 json 哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文