zl程序教程

您现在的位置是:首页 >  后端

当前栏目

PHP json_decode 无法解析,那就试试 Services_JSON

PHPJSONJSON 解析 无法 试试 decode Services
2023-06-13 09:18:33 时间

PHP 的 json_decode 函数不知道是有bug,还是考虑太少,常常会发生解析不出数据的情况,使用 json_last_error_msg() 函数大部分情况下可以获得下面的错误:

Control character error, possibly incorrectly encoded

但是 Google 了一圈,都没有办法去修正这个错误,后面咨询了牛逼闪闪的 Kingmax 师兄,他说:“json_decode经常会有东西解不出来的,应该用一个叫Sevice_JSON的类去解析,这个比较全能,帮你把特殊字符都处理好了”,试了下果然可以。

到这里直接下载 Services_JSON,然后直接使用:

<?php
include 'JSON.php';
$json = new Services_JSON();
$data = $json->decode($str);
?>

既可以了,如果想和 json_decode 的第二个参数一样,可以解析返回的数据为数组,那就要在 new Services_JSON 的时候加上这个参数 SERVICES_JSON_LOOSE_TYPE

<?php
include 'JSON.php';
$json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
$data = $json->decode($str);
?>