Home > Backend Development > PHP Tutorial > json_decode为空问题

json_decode为空问题

PHPz
Release: 2018-10-09 14:54:16
forward
2084 people have browsed it

<span style="font-family: 微软雅黑,Microsoft YaHei;">在error_log日志中打出 json_last_error() 和 json_last_error_msg()</span>

<span style="font-family: 微软雅黑,Microsoft YaHei;">发现 4 和 'Syntax error'</span><span style="font-family: 微软雅黑,Microsoft YaHei;"><br></span>

<span style="font-family: 微软雅黑,Microsoft YaHei;">可以用base64_encode记录下要解的$response数据  然后在本地base64_decode出来 </span><code><span style="font-family: 微软雅黑,Microsoft YaHei;">再进行json_decode</span>  查为什么不能解开<br>

<span style="font-family: 微软雅黑,Microsoft YaHei;">测试了</span>

<code class="Brush variable"><span style="font-family: 微软雅黑,Microsoft YaHei;">$json</span> <code class="Brush plain"><span style="font-family: 微软雅黑,Microsoft YaHei;">= iconv(</span><span style="font-family: 微软雅黑,Microsoft YaHei;">'GBK'</span><code class="Brush plain"><span style="font-family: 微软雅黑,Microsoft YaHei;">,</span><span style="font-family: 微软雅黑,Microsoft YaHei;">'utf-8'</span><code class="Brush plain"><span style="font-family: 微软雅黑,Microsoft YaHei;">,</span><code class="Brush variable"><span style="font-family: 微软雅黑,Microsoft YaHei;">$json</span><code class="Brush plain"><span style="font-family: 微软雅黑,Microsoft YaHei;">);</span>

<code class="Brush plain">$json = stripslashes($json);<br>

<code class="Brush plain"><span style="font-family: 微软雅黑,Microsoft YaHei;">$json = htmlspecialchars_decode($json);</span>

<span style="font-family: 微软雅黑,Microsoft YaHei;">等多种方法 都不行 </span><br>

<span style="font-family: 微软雅黑,Microsoft YaHei;">最后发现返回的response数据有bom头字符</span><br>

$response = trim($response,chr(239).chr(187).chr(191));<br>$response = json_decode($response, true);<br>

<span style="font-family: 微软雅黑,Microsoft YaHei;">这样正常解开了</span><br>

<code><span style="font-family: 微软雅黑,Microsoft YaHei;">base64_decode出来</span>的json数据有没有bom头字符 除了网上说的其它方法  也可以放到json在线编辑器中查看<br>

附:<br>

json_decode要求的字符串比较严格:<br>(1)使用UTF-8编码<br>(2)不能在最后元素有逗号<br>(3)不能使用单引号<br>(4)不能有\r,\t,如果有请替换

更多相关教程请访问 php编程从入门到精通全套视频教程<br>

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template