json_decode PHP 的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6324645/
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
Problem with json_decode PHP
提问by Francisco Gutiérrez
I'm receiving a json array from python as the return of curl_exec in PHP (first json PHP -> python, that returns another json), and decode fails due to bad syntax. The json received is valid, but somehow if I cast this json to string and prints it I get a string with 29 characters, but if I print strlen((string)$my_json) it says 50.
我从 python 接收一个 json 数组作为 PHP 中 curl_exec 的返回(第一个 json PHP -> python,返回另一个 json),由于语法错误,解码失败。收到的 json 是有效的,但不知何故,如果我将此 json 转换为字符串并打印它,我会得到一个包含 29 个字符的字符串,但是如果我打印 strlen((string)$my_json) 它说 50。
Here's the code:
这是代码:
$results = curl_exec($ch);
curl_close($ch);
var_dump(json_decode($results));
And that returns NULL. If I do the following
并且返回NULL。如果我执行以下操作
echo (string)$results;
It prints [[11, "stuffstuf", "stuffs"]] (29 chars), which is a valid json. But if I do
它打印 [[11, "stuffstuf", "stuffs"]] (29 个字符),这是一个有效的 json。但如果我这样做
echo strlen((string)$results);
It prints 50.
它打印 50。
So, I don't know what is going on. Any thoughts would be appreciated =)
所以,我不知道发生了什么。任何想法将不胜感激=)
回答by Seldaek
Could it be that you have some html tags around it that you don't see when doing the simple echo?
可能是您周围有一些 html 标签,而您在执行简单回显时看不到这些标签吗?
Try: echo htmlentities((string)$results);
to see more, or check the html source of the page.
尝试:echo htmlentities((string)$results);
查看更多信息,或查看页面的 html 源代码。
If json_decode()
fails, it means the string isn't standard JSON.
如果json_decode()
失败,则表示该字符串不是标准的 JSON。
You can also use json_last_error_msg()to figure out why it returned NULL. That will return an error message if there was any error in json_decode.
您还可以使用json_last_error_msg()找出返回 NULL 的原因。如果 json_decode 中有任何错误,这将返回一条错误消息。
回答by tparton42
Seldaek's answer was a big help, and I believe it to be the overall best answer.
Seldaek 的回答很有帮助,我相信这是总体最佳答案。
I have encountered a similar issue, caused by using single quotes instead of double quotes, and the differences between the latest supported version of PHP on Red Hat, versus PHP 5.5 on Ubuntu.
我遇到了类似的问题,这是由使用单引号而不是双引号引起的,以及 Red Hat 上支持的最新 PHP 版本与 Ubuntu 上的 PHP 5.5 之间的差异。
PHP on RHEL returned "Invalid or malformed JSON" when reading this next line in from a file, where as it was fine on my Ubuntu PHP 5.5 instance.
从文件中读取下一行时,RHEL 上的 PHP 返回“无效或格式错误的 JSON”,而在我的 Ubuntu PHP 5.5 实例上则很好。
{ 'book': 'Dune', 'author': 'Frank Herbert', 'ISBN-13': '978-0441172719' }
Changing to double quotes, like below, resolved my issue
更改为双引号,如下所示,解决了我的问题
{ "book": "Dune", "author": "Frank Herbert", "ISBN-13": "978-0441172719" }
回答by Alp Altunel
its possible to print out json_last_error_msg() which gives error in text format not code. So no need to use switch and error handling.
可以打印出 json_last_error_msg() ,它以文本格式而不是代码给出错误。所以不需要使用开关和错误处理。
回答by Matija
Seldaek said right, use of json_last_error
is very good. I also use stripslashes
before json_decode
. Here is my code:
Seldaek说的对,使用的json_last_error
很好。我stripslashes
之前也用过json_decode
。这是我的代码:
$resp = stripslashes($resp);
$resp_json = json_decode($resp);
switch(json_last_error())
{
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Invalid or malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
}
After that when you debug
and you still have let's say error 4 - JSON_ERROR_SYNTAX. get VALUE
of variable $resp
in debug mode and paste it to free web tool for JSON conversion @ Json conversion check - jsonlint. And check what's the deal with your conversion.
在那之后,当您debug
和您仍然有错误 4 - JSON_ERROR_SYNTAX 时。在调试模式下获取VALUE
变量$resp
并将其粘贴到用于 JSON 转换的免费 Web 工具@Json 转换检查 - jsonlint。并检查您的转换是怎么回事。