Home >Backend Development >PHP Problem >What to do if php json parsing error occurs

What to do if php json parsing error occurs

藏色散人
藏色散人Original
2021-03-05 09:13:413768browse

php json parsing error solution: first create a PHP sample file; then manually obtain the specific error of json parsing through the "json_last_error()" function; finally modify it according to the error.

What to do if php json parsing error occurs

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

PHP parsing json, xml error

Parsing json

php built-in function json_decode() can parse json string

But sometimes the json that looks correct is not parsed Always returns null.

Did you know that json may fail to be parsed, and PHP will not generate a prompt at this time.

We need to manually obtain it through the json_last_error() function

function json_decode_siam($string, $mark = false){
    $data = json_decode($string, $mark);
    switch (json_last_error()) {
        case JSON_ERROR_NONE:
            return $data;
            break;
        case JSON_ERROR_DEPTH:
            echo ' - 已超出最大堆栈深度';
            break;
        case JSON_ERROR_STATE_MISMATCH:
            echo ' - JSON无效或格式错误  状态不匹配';
            break;
        case JSON_ERROR_CTRL_CHAR:
            echo ' - 发现意外的控制字符 可能编码错误';
            break;
        case JSON_ERROR_SYNTAX:
            echo ' - 错误符号,json格式错误';
            break;
        case JSON_ERROR_UTF8:
            echo ' - 格式错误的UTF-8字符,可能是错误编码的';
            break;
        default:
            echo ' - Unknown error';
            break;
    }
}

Parsing xml

In php, there are several ways to parse xml, mainly relying on Different extension environments.

Here I will talk about this method that I often use

simplexml_load_string();
simplexml_load_file();

You can load and parse through strings or files, and return Simplexml objects

In this method , if the xml format is wrong, an error will be generated directly

$str = "不是xml字符串";
$data = simplexml_load_string($str);
var_dump($data);

get

bool(false)
PHP Warning:  simplexml_load_string(): Entity: line 1: parser error : Start tag expected, &#39;<&#39; not found in /usercode/file.php on line 4
PHP Warning:  simplexml_load_string(): 不是xml字符串 in /usercode/file.php on line 4
PHP Warning:  simplexml_load_string(): ^ in /usercode/file.php on line 4

This is a PHP error, not an exception, so try{}catch(){) cannot be used

This part of the knowledge may be improved in the future (mainly because seniors have written many articles)

[Recommended study: "PHP Video Tutorial"]

The above is the detailed content of What to do if php json parsing error occurs. For more information, please follow other related articles on the PHP Chinese website!

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