开发者

Converting "complex" JSON data in to a hash

开发者 https://www.devze.com 2023-02-15 14:06 出处:网络
I am using Ruby on Rails 3 and I am trying to convert the following 开发者_如何转开发JSON data

I am using Ruby on Rails 3 and I am trying to convert the following 开发者_如何转开发JSON data

"{\"errors\":{\"base\":\"Invalid field.\"}}"

in to a hash accessible this way:

@internal_test1[:errors][:base]
# => "Invalid field"

How to do that?


UPDATE

@internal_test1 = "{\"errors\":{\"base\":\"Invalid email/password combination.\"}}"
test = ActiveSupport::JSON.decode(@internal_test1)
@internal_test2 = test["errors"]

The debug is

@internal_test1
--- "{\"errors\":{\"base\":\"Invalid email/password combination.\"}}"

@internal_test2
--- 
base: Invalid email/password combination.


> @internal_test1 = ActiveSupport::JSON.decode "{\"errors\":{\"base\":\"Invalid field.\"}}"
 => {"errors"=>{"base"=>"Invalid field."}} 
> @internal_test1["errors"]
 => {"base"=>"Invalid field."} 
0

精彩评论

暂无评论...
验证码 换一张
取 消