在用json_decode对JSON格式的字符串进行解码时为空,查阅资料才得知UTF-8的BOM头的原因
json_decode要求的字符串比较严格:
(1)使用UTF-8编码
(2)不能在最后元素有逗号
(3)不能使用单引号
(4)不能有\\r,\\t,如果有请替换
解决方法1:
PHP
$info json_decodetrim$infochr239chr187chr191true
解决方法2:
PHP
匹配是否有BOM头,存在则截取
ifpreg_match'/^\xEF\xBB\xBF/'$info $json_data substr$info3$info json_decodetrim$info true
1
如何用php调用外部接口json数据
一般使用php发送请求,获取返回的数据,进行解析;<?php$url=接口地址;//发送请求获取返回值,file_get_contents只支持get请求,post使用curl$json = file_get_contents($url);//把json数据转化成数组$data = json_decode($json,true);//打印看看print_r($data);?>
php如何读取json中的数据?
先使用json_decode()函数解析成数组,在用变量等于解析出来的数组 $test=json_decode(json数据);输出 echo $test->key;
你 发表评论:
欢迎