Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the common methods of php json

Shulou Source: shulou.com Published: 2022-06-03 10:38:34 11月15日 Update

This article shows you what the common methods of php json are, which are concise and easy to understand, which can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Php json commonly used methods: 1, json_encode (), used to decode the string in JSON format; 2, json_encode (), used to decode the string in JSON format; 3, json_last_error (), used to return the last error.

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

Common methods of php json:

1. Json_encode ()

PHP json_encode () is used to JSON the variable, and the function returns JSON data if the execution succeeds, otherwise it returns FALSE.

Grammar

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

Example:

Output result:

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

2. Json_encode ()

The json_decode () function is used to decode a string in JSON format and convert it to a PHP variable.

Syntax:

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

Parameters:

Json_string: the JSON string to be decoded, which must be UTF-8 encoded data

Assoc: an array is returned when this parameter is TRUE, and an object is returned when FALSE.

Depth: an integer type parameter that specifies the recursion depth

Options: binary mask, currently only supports JSON_BIGINT_AS_STRING.

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-returns the last error that occurred

Syntax:

Json_last_error ()

If so, returns the last error that occurred during JSON encoding and decoding. An integer (integer) is returned, which will be one of the following constants:

JSON error code constant meaning usability JSON_ERROR_NONE no error occurred

JSON_ERROR_DEPTH has reached the maximum stack depth

Invalid or abnormal JSON_ERROR_STATE_MISMATCH JSON

JSON_ERROR_CTRL_CHAR control character error, may be incorrect encoding

JSON_ERROR_SYNTAX syntax error

The abnormal UTF-8 character of JSON_ERROR_UTF8 may be due to incorrect encoding. The type specified by PHP 5.3.3JSON_ERROR_RECURSIONOne or more recursive references in the value to be encodedPHP 5.5.0JSON_ERROR_INF_OR_NANOne or more NAN or INF values in the value to be encodedPHP 5.5.0JSON_ERROR_UNSUPPORTED_TYPE, the value cannot be encoded. The property name specified by PHP 5.5.0JSON_ERROR_INVALID_PROPERTY_NAME cannot be encoded. PHP 7.0.0JSON_ERROR_UTF16 malformed UTF-16 characters, probably because of incorrect character encoding. PHP 7.0.0

Example:

Tags: Characters codes errors commonly used methods strings grammars parameters formats examples results output content functions variables constants skills data depth knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Docker MariaDB NVidia Apple