Home  >  Article  >  Backend Development  >  What are the common methods of php json

What are the common methods of php json

青灯夜游
青灯夜游Original
2021-09-18 16:56:422170browse

php Common json methods: 1. json_encode(), used to decode strings in JSON format; 2. json_encode(), used to decode strings in JSON format; 3. json_last_error() , used to return the last error that occurred.

What are the common methods of php json

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

php json common methods :

1. json_encode()

PHP json_encode() is used to JSON encode variables. This function returns JSON data if executed successfully. , otherwise return FALSE.

Syntax

string json_encode ( $value [, $options = 0 ] )

Example:

<?php
   $arr = array(&#39;a&#39; => 1, &#39;b&#39; => 2, &#39;c&#39; => 3, &#39;d&#39; => 4, &#39;e&#39; => 5);
   echo json_encode($arr);
?>

Output result:

{"a":1,"b":2,"c":3,"d":4,"e":5}

2, json_encode()

json_decode() function is used to decode JSON format strings and convert them into PHP variables.

Syntax:

mixed json_decode ($json_string [,$assoc = false [, $depth = 512 [, $options = 0 ]]])

Parameters:

  • json_string: JSON string to be decoded, must be UTF-8 encoded data

  • assoc: When this parameter is TRUE, an array will be returned, and when FALSE, an object will be returned.

  • depth: Integer type parameter, which specifies the recursion depth

  • options: Binary mask, currently only JSON_BIGINT_AS_STRING is supported.

Example:

Output result:

object(stdClass)#1 (5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

array(5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

3, json_last_error()

json_last_error — Return the last error that occurred

Syntax:

json_last_error()

If any, return the last error that occurred during JSON encoding and decoding. An integer will be returned, and this value will be one of the following constants:

No error occurred##JSON_ERROR_STATE_MISMATCHJSON_ERROR_CTRL_CHARJSON_ERROR_SYNTAX##JSON_ERROR_UTF8JSON_ERROR_RECURSIONJSON_ERROR_INF_OR_NAN##JSON_ERROR_UNSUPPORTED_TYPEJSON_ERROR_INVALID_PROPERTY_NAMEJSON_ERROR_UTF16
<?php
// 一个有效的 json 字符串
$json[] = &#39;{"Organization": "PHP Documentation Team"}&#39;;

// 一个无效的 json 字符串会导致一个语法错误,在这个例子里我们使用 &#39; 代替了 " 作为引号
$json[] = "{&#39;Organization&#39;: &#39;PHP Documentation Team&#39;}";


foreach ($json as $string) {
    echo &#39;Decoding: &#39; . $string;
    json_decode($string);

    switch (json_last_error()) {
        case JSON_ERROR_NONE:
            echo &#39; - No errors&#39;;
        break;
        case JSON_ERROR_DEPTH:
            echo &#39; - Maximum stack depth exceeded&#39;;
        break;
        case JSON_ERROR_STATE_MISMATCH:
            echo &#39; - Underflow or the modes mismatch&#39;;
        break;
        case JSON_ERROR_CTRL_CHAR:
            echo &#39; - Unexpected control character found&#39;;
        break;
        case JSON_ERROR_SYNTAX:
            echo &#39; - Syntax error, malformed JSON&#39;;
        break;
        case JSON_ERROR_UTF8:
            echo &#39; - Malformed UTF-8 characters, possibly incorrectly encoded&#39;;
        break;
        default:
            echo &#39; - Unknown error&#39;;
        break;
    }

    echo PHP_EOL;
}
?>
Output result:
JSON error code
Constant Meaning Availability
##JSON_ERROR_NONE
##JSON_ERROR_DEPTHMaximum stack depth reached
Invalid or abnormal JSON
The control character is wrong, maybe the encoding is wrong
Syntax error
Exceptional UTF-8 characters, perhaps due to incorrect encoding. PHP 5.3.3
One or more recursive references in the value to be encodedPHP 5.5.0
One or more NAN or INF values ​​in the value to be encodedPHP 5.5.0
specified Type, value cannot be encoded. PHP 5.5.0
The specified property name cannot be encoded. PHP 7.0.0
Malformed UTF-16 characters, possibly because The character encoding is incorrect. PHP 7.0.0 Example:
Decoding: {"Organization": "PHP Documentation Team"} - No errors
Decoding: {&#39;Organization&#39;: &#39;PHP Documentation Team&#39;} - Syntax error, malformed JSON

Recommended learning: "

PHP Video Tutorial

"

The above is the detailed content of What are the common methods of php json. 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