json_decode returns JSON_ERROR_SYNTAX but online formatter says the JSON is OK
我遇到了一个非常奇怪的问题。
我有一个JSON网络服务。
当我在此网站上进行检查时http://www.freeformatter.com/json-formatter.html#ad-output
一切都很好。
但是当我使用以下代码加载JSON时:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | $data = file_get_contents('http://www.mywebservice'); if(!empty($data)) { $obj = json_decode($data); switch (json_last_error()) { case JSON_ERROR_NONE: echo ' - JSON_ERROR_NONE'; break; case JSON_ERROR_DEPTH: echo ' - JSON_ERROR_DEPTH'; break; case JSON_ERROR_STATE_MISMATCH: echo ' - JSON_ERROR_STATE_MISMATCH'; break; case JSON_ERROR_CTRL_CHAR: echo ' - JSON_ERROR_CTRL_CHAR'; break; case JSON_ERROR_SYNTAX: echo"\ \ \ \ - SYNTAX ERROR \ \ \ \ "; break; case JSON_ERROR_UTF8: echo ' - JSON_ERROR_UTF8'; break; default: echo ' - Unknown erro'; break; } |
我得到了错误:语法错误
根本无法满足的需求。
这是一场噩梦。
我看到在PHP 5.5中我可以使用此功能:http://php.net/manual/en/function.json-last-error-msg.php
(但是我还没有成功安装PHP 5.5,并且我不确定该功能是否会给我更多细节)
我遇到了同样的问题,实际上有一些看不见的隐藏字符,您需要将其删除。
以下是适用于许多情况的全局代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?php $checkLogin = file_get_contents("http://yourwebsite.com/JsonData"); // This will remove unwanted characters. // Check http://www.php.net/chr for details for ($i = 0; $i <= 31; ++$i) { $checkLogin = str_replace(chr($i),"", $checkLogin); } $checkLogin = str_replace(chr(127),"", $checkLogin); // This is the most common part // Some file begins with 'efbbbf' to mark the beginning of the file. (binary level) // here we detect it and we remove it, basically it's the first 3 characters if (0 === strpos(bin2hex($checkLogin), 'efbbbf')) { $checkLogin = substr($checkLogin, 3); } $checkLogin = json_decode( $checkLogin ); print_r($checkLogin); ?> |
删除
1 2 3 4 5 6 |
您不应该有BOM,但是如果有BOM,则它是不可见的,因此您将看不到它!
有关HTML中BOM表的信息,请参见W3C。
如果要修复的文件很多,请使用BOM清理器。
我解决了这个问题,在json_decode之前在字符串中添加了反斜杠。
1 2 |
<pa>I had same issue. For me it was causing by
<pa>At the beginning of file I had decleared break line tag... So this was error for me. Removing this break line tag , I was able to [...]<pb>
我这边的一个问题是,有一些以0开头的无效数字,例如:" 001"," 002"," 003"。
1 2 3 | "expectedToBeReturned":1, "inventoryNumber":001, "remindNote":"", |
将001替换为1即可。
我也遇到了这个问题,这让我感到非常沮丧。经过数小时尝试在互联网上使用其他解决方案。我注意到该文件的编码使用UTF-8和BOM,因为var_dump()在JSON之前回显了一个奇怪的字符。
我将正在使用的sample.json文件从带有BOM的UTF-8转换为UTF-8 ...在VS CODE中,将以下内容添加到您的settings.json中,或确保以下设置代码如下所示(因此,默认情况下,您创建的任何文件都将以UTF-8编码;
1 | "files.encoding":"utf8", |
然后,您将在VSCode工具栏上看到类似以下屏幕截图的内容。 (要使
但是在我的情况下,我创建的JSON文件具有带有BOM编码的UTF-8,这就是为什么当我执行
这将使用UTF-8编码重新保存文件,您可以继续检查代码。
快乐编码!
<pa>I had the same issues. I took the following steps:<pb>
<MMKG1>
<pa>changed the JSON text encoding<pb>
<zzu7>
<MMKGX1>
<MMKG1>
<pa>I then viewed the plain text before decoding. I found crazy symbols like <pb>
<pa>
引起的
然后我将其剥离
1 |
并且我成功解码了JSON
您没有显示JSON,但是听起来好像参数中的UTF-8序列无效,大多数在线验证器都不会捕获它。
确保您的数据为UTF-8,并检查是否有外来字符。
您不需要PHP5来查看错误,请使用error_log()记录问题。
JSON字符串必须用双引号引起来,JSON无效,因为您不需要转义
1 2 3 4 5 6 7 8 9 10 11 | char = unescaped / escape ( %x22 / ;" quotation mark U+0022 %x5C / ; \\ reverse solidus U+005C %x2F / ; / solidus U+002F %x62 / ; b backspace U+0008 %x66 / ; f form feed U+000C %x6E / ; n line feed U+000A %x72 / ; r carriage return U+000D %x74 / ; t tab U+0009 %x75 4HEXDIG ) ; uXXXX U+XXXX |
请参阅以下用于JSON的特殊字符列表:
1 2 3 4 5 6 7 8 9 | \\b Backspace (ascii code 08) \\f Form feed (ascii code 0C) \ New line \ Carriage return \\t Tab " Double quote \\\\ Backslash character |
查看此站点以获取更多文档。
我遇到了同样的问题,原因是响应的文本看起来像json,但实际上是HTML格式的文本。您可以以JSON格式回显文本(类似于json),以查看内部的实际内容:
1 2 3 | $response = file_get_contents('http://www.mywebservice'); header('Content-Type: application/json'); echo $response; |
此函数file_get_contents将返回一些额外的HTML代码。
就我而言,我删除了那些不需要的字符:
1 2 | $response = str_replace('<head></head><body>[cc lang="php"]', '',$response ); $response = str_replace(' |
body>','',$ response);
code> pre>
这是完整的代码:
1 2 3 | $response = file_get_contents('http://www.mywebservice'); $response = str_replace('<head></head><body>[cc lang="php"]', '',$response ); $response = str_replace(' |
body>','',$ response);
$ response = json_decode($ response);
header('Content-Type:application / json');
$ error = json_last_error_msg();
回声$错误;
if($ error == null){echo"这确实是一个JSON:"}
echo $ response;
code> pre>
我遇到了同样的问题,收到了JSON_ERROR_CTRL_CHAR和JSON_ERROR_SYNTAX。
这是我的解决方法。
1 |
n
在我的情况下:
json_decode(html_entity_decode($ json_string));
此代码对我有用。
基本上,它会删除隐藏的字符。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | function cleanString($val) { $non_displayables = array( '/%0[0-8bcef]/', # url encoded 00-08, 11, 12, 14, 15 '/%1[0-9a-f]/', # url encoded 16-31 '/[\\x00-\\x08]/', # 00-08 '/\\x0b/', # 11 '/\\x0c/', # 12 '/[\\x0e-\\x1f]/', # 14-31 '/x7F/' # 127 ); foreach ($non_displayables as $regex) { $val = preg_replace($regex,'',$val); } $search = array("\\0","\ ","\\x1a","\\t"); return trim(str_replace($search,'',$val)); } |
请先清除json数据,然后再加载。
<pa>After trying all the solution without the result this is the one worked for me.<pb>
<pa>Hope it will help someone<pb>
<zzu6>
我有同样的问题。对我来说,这是由
<pa>Example usage:<pb>
<zzu4>