Home >Backend Development >PHP Tutorial >By obtaining user information in batches from WeChat, you can get json data, but when you use json_decode to convert it into an array, it returns null (solved)

By obtaining user information in batches from WeChat, you can get json data, but when you use json_decode to convert it into an array, it returns null (solved)

WBOY
WBOYOriginal
2016-12-01 01:27:251305browse

<code>https_request是调用第三方接口的方法。</code>

打印$output是有值的,能看到access_token,但是打印$jsoninfo的时候就为空了,所以后面的返回access_token也获取不到

<code>        $output =$this->https_request($url);
            $jsoninfo = json_decode($output, true);
            $access_token = $jsoninfo["access_token"];
            return $access_token; 
            
</code>

问题已解决
原因:在获取第三方接口的方法中,已经有json_decode了

回复内容:

<code>https_request是调用第三方接口的方法。</code>

打印$output是有值的,能看到access_token,但是打印$jsoninfo的时候就为空了,所以后面的返回access_token也获取不到

<code>        $output =$this->https_request($url);
            $jsoninfo = json_decode($output, true);
            $access_token = $jsoninfo["access_token"];
            return $access_token; 
            
</code>

问题已解决
原因:在获取第三方接口的方法中,已经有json_decode了

也就是说 json_decode 失败了。
json_decode 之后用 json_last_error() 函数看一下是什么错误。

<code>1. json字符串必须以双引号包含
$output = str_replace("'", '"', $output);
2. json字符串必须是utf8编码
$output = iconv('gbk', 'utf8', $output);
3.不能有多余的逗号 如:[1,2,]
用正则替换掉,preg_replace('/,\s*([\]}])/m', '$1', $output)</code>

已找到问题,个人原因

Statement:
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