简体   繁体   English

在 Ruby on Rails 中将字符串哈希转换为 json

[英]String hash to json in Ruby on Rails

string = "{:from_zip_code=>\"90066\", :to_zip_code=>\"12345\", [{:amount=>\"2.8\", :add_on_type=>\"US-A-RR\", :requires_all_of=>{:requires_one_of=>{:add_on_type_v7=>[\"US-A-COD\"]}}, {:amount=>\"12.2\", :add_on_type=>\"US-A-REG\", :prohibited_with_any_of=>{:add_on_type_v7=>[\"SC-A-HP\"]}}, {:amount=>\"5.15\", :add_on_type=>\"US-A-RD\", :requires_all_of=>{:requires_one_of=>{:add_on_type_v7=>[\"US-A-COD\"]}}, :prohibited_with_any_of=>{:add_on_type_v7=>[\"SC-A-HP\"]}}, {:add_on_type=>\"SC-A-INS\", :prohibited_with_any_of=>{:add_on_type_v7=>[\"US-A-REG\", \"US-A-INS\"]}, :missing_data=>\"InsuredValue\"}, {:add_on_type=>\"US-A-NND\"}]}}"

Could you please help me to make this string as json or hash like below你能帮我把这个字符串做成json或hash吗

{ :from_zip_code => "90066",
  :to_zip_code => "12345",
  [
    {
      :amount => "2.8",
      ...
    }
  ]
}
 gem install json

You would require the gem in your code like this:您需要在代码中使用 gem,如下所示:

 require 'rubygems'
 require 'json'

Then you can parse your JSON string like this:然后你可以像这样解析你的 JSON 字符串:

JSON.parse(string)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM