json_decode PHP问题 [英] Problem with json_decode PHP
问题描述
我正在从python接收一个json数组,作为PHP中curl_exec的返回(第一个json PHP-> python,它返回另一个json),并且由于语法错误而解码失败.接收到的json是有效的,但是以某种方式,如果我将此json转换为字符串并打印,则会得到一个包含29个字符的字符串,但是如果我打印strlen((string)$ my_json),它会显示50.
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.
代码如下:
$results = curl_exec($ch);
curl_close($ch);
var_dump(json_decode($results));
然后返回NULL.如果我执行以下操作
And that returns NULL. If I do the following
echo (string)$results;
它打印[[11,"stuffstuf","stuffs"]](29个字符),它是一个有效的json.但是如果我这样做
It prints [[11, "stuffstuf", "stuffs"]] (29 chars), which is a valid json. But if I do
echo strlen((string)$results);
它打印50.
所以,我不知道发生了什么.任何想法将不胜感激=)
So, I don't know what is going on. Any thoughts would be appreciated =)
推荐答案
是否可能是您周围有一些html标记,在执行简单的echo时看不到?
Could it be that you have some html tags around it that you don't see when doing the simple echo?
尝试:echo htmlentities((string)$results);
查看更多内容,或检查页面的html源.
Try: echo htmlentities((string)$results);
to see more, or check the html source of the page.
如果json_decode()
失败,则表明该字符串不是标准JSON.
If json_decode()
fails, it means the string isn't standard JSON.
您还可以使用 json_last_error_msg()找出为什么它返回NULL的原因.如果json_decode中有任何错误,则会返回一条错误消息.
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_decode PHP问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!