\"Study/Control: ABIRATERONE ACETATE - 20151413355\", \"left_mue_start_date\-6ren">
gpt4 book ai didi

ruby-on-rails - JSON::ParserError: 757: '{ 处的意外标记

转载 作者:数据小太阳 更新时间:2023-10-29 06:55:20 24 4
gpt4 key购买 nike

当前哈希是

{\"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

最佳答案

您拥有的是打印为字符串的散列。要将其转换为哈希,请使用 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}"
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::ParserError 因为在非字符串上使用 JSON.parse -JSON 字符串:

"{\"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}" 

关于ruby-on-rails - JSON::ParserError: 757: '{ 处的意外标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29047089/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com