2015-06-24 60 views
-2
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\"}]}}" 

能否请你帮我做这个字符串作为JSON或哈希像下面字符串哈希在Ruby中JSON on Rails的

{ :from_zip_code => "90066", 
    :to_zip_code => "12345", 
    [ 
    { 
     :amount => "2.8", 
     ... 
    } 
    ] 
} 
+0

你尝试过什么吗? –

+0

@Зелёный,是的,我已经在我的水平尝试过它,但我无法将其作为json或哈希。 – ROR

+0

我有一个坏主意,调用eval(字符串)将使它。但这并不安全。 – jerrytao

回答

0
gem install json 

你需要在你的代码是这样的宝石:

require 'rubygems' 
require 'json' 

然后你可以分析你的JSON字符串是这样的:

JSON.parse(string) 
+0

JSON.parse(字符串),这对我来说工作不正常。 – ROR