ruby-on-rails – JSON :: ParserError:757:'{的意外令牌

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – JSON :: ParserError:757:'{的意外令牌前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当前的哈希是
{\"report_name\"=>\"Study/Control: ABIRATERONE ACETATE - 20151413355\",\"left_mue_start_date\"=>\"02-26-2015\",\"left_mue_end_date\"=>\"03-19-2015\",\"right_mue_start_date\"=>\"02-26-2015\",\"right_mue_end_date\"=>\"03-19-2015\",\"report_formulary_id\"=>\",7581\",\"mue\"=>\"true\",\"mue_type\"=>\"study/control\",\"chain_id\"=>\"1\",\"left_mue_formulary_ids\"=>[\"7581\"],\"action\"=>\"create_report\",\"controller\"=>\"informatics\",\"user_id\"=>339}

现在我需要将其转换为适当的哈希值

{"report_name" => "Study/Control: ABIRATERONE ACETATE - 20151413355"}

所以我试图用JSON.parse得到它,但我得到的错误如下:

JSON::ParserError: 757: unexpected token at '{

所以,如果有人知道这一点,请帮助我.
我正在使用Rails 3.2

解决方法

你有一个打印为String的哈希.要将其转换为Hash,请使用 eval.
ch = "{\"report_name\"=>\"Study/Control: ABIRATERONE ACETATE - 20151413355\",\"user_id\"=>339}"
hash = eval(ch)
# => {"report_name"=>"Study/Control: ABIRATERONE ACETATE - 20151413355","left_mue_start_date"=>"02-26-2015","left_mue_end_date"=>"03-19-2015","right_mue_start_date"=>"02-26-2015","right_mue_end_date"=>"03-19-2015","report_formulary_id"=>",7581","mue"=>"true","mue_type"=>"study/control","chain_id"=>"1","left_mue_formulary_ids"=>["7581"],"action"=>"create_report","controller"=>"informatics","user_id"=>339}

PS:JSON字符串应如下所示,这意味着您拥有的不是JSON,因此您在非JSON字符串上使用JSON.parse时获得了JSON :: ParserError:

"{\"report_name\":\"Study/Control: ABIRATERONE ACETATE - 20151413355\",\"left_mue_start_date\":\"02-26-2015\",\"left_mue_end_date\":\"03-19-2015\",\"right_mue_start_date\":\"02-26-2015\",\"right_mue_end_date\":\"03-19-2015\",\"report_formulary_id\":\",\"mue\":\"true\",\"mue_type\":\"study/control\",\"chain_id\":\"1\",\"left_mue_formulary_ids\":[\"7581\"],\"action\":\"create_report\",\"controller\":\"informatics\",\"user_id\":339}"

猜你在找的Ruby相关文章