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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29047089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:08:07  来源:igfitidea点击:

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

ruby-on-railsrubyjson

提问by rajeev

the current hash is

当前的哈希是

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

now I need to convert it in proper hash like

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

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

so I am trying to get it with JSON.parse but I am getting error like:

所以我试图用 JSON.parse 来获取它,但我收到如下错误:

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

So if someone know about that so please help me. and I am using Rails 3.2

因此,如果有人知道这一点,请帮助我。我正在使用 Rails 3.2

回答by shivam

What you have is a hash printed as String. To convert it into a Hash use eval.

你所拥有的是一个打印为字符串的哈希。要将其转换为哈希,请使用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: A JSON string should look as follows, meaning what you have is not JSON and hence you got JSON::ParserErrorfor using JSON.parseon a non-JSON string :

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

回答by Juan Felipe Rodriguez

To avoid using evalyou could use JSON.parse ch.gsub('=>', ':')this way you will get a HASHfrom your HASHstored as STRING

为了避免使用eval您可以使用 JSON.parse ch.gsub('=>', ':')这种方式,您HASH将从您的HASH存储中获得一个STRING

回答by Mathew P. Jones

Last time when I got this issue, since the json file that I got from a API that contains a BOM

上次我遇到这个问题时,因为我从包含一个 API 的 json 文件 BOM

UTF-8 BOM is a sequence of bytes (EF BB BF) What's different between UTF-8 and UTF-8 without BOM?

UTF-8 BOM 是一个字节序列 (EF BB BF) UTF-8 和没有 BOM 的 UTF-8 有什么区别?

at the beginning of that string, but you know that part wouldn't display or as readable when we got the string from response. I try to use Ruby JSON to parse it, but I failed, I got the same exception with yours. just a reminder for others, when you get a Json response. By the way, this would be no problem while you are handling that in javascript, but with problems in Python or Ruby languages.

在该字符串的开头,但您知道当我们从响应中获取字符串时,该部分不会显示或可读。我尝试使用 Ruby JSON 来解析它,但我失败了,我遇到了与您相同的异常。当您收到 Json 响应时,只是对其他人的提醒。顺便说一句,当您在 javascript 中处理它时,这不会有问题,但在 Python 或 Ruby 语言中会出现问题。

回答by Louis Cribbins

I ran into a similar problem, though it was failing while parsing \"\". This is in regards to using Pact.IO. I mention it here since this is the highest ranked Google result while looking for the error I encountered. The solution in my case was to change the body of a POST in my C# application so that it wasn't using empty string, but a null string. Basically I added this before my HTTP call.

我遇到了类似的问题,尽管它在解析 \"\" 时失败了。这是关于使用 Pact.IO。我在这里提到它,因为这是在查找我遇到的错误时排名最高的 Google 结果。我的解决方案是在我的 C# 应用程序中更改 POST 的主体,以便它不使用空字符串,而是使用空字符串。基本上我在我的 HTTP 调用之前添加了这个。

if (request.Method == HttpMethod.Post && request.Content!=null && request.Content.GetType()==typeof(StringContent) && request.Content.ReadAsStringAsync().Result == String.Empty)
  request.Content = new StringContent(null);