How to use JSON parsing and generating functions in PHP?
Introduction:
JSON (JavaScript Object Notation) is a lightweight data exchange format that has gradually become a commonly used data format in Web development. In PHP, we can use the built-in JSON function for JSON parsing and generation. This article will introduce how to use JSON parsing and generation functions in PHP and give corresponding code examples.
1. JSON parsing function
- json_decode function
json_decode function is used to parse JSON strings into PHP data types. Its basic syntax is as follows:
mixed json_decode(string $json[, bool $assoc = FALSE[, int $depth = 512[, int $options = 0]]])
Parameters Description:
- $json: JSON string to be parsed
- $assoc: optional parameter, specifies whether to return an associative array, the default is FALSE to return an object
- $depth: Optional parameter, specifies the recursion depth limit, the default is 512
- $options: Optional parameter, specifies additional parsing options, the default is 0
Example 1:
1 2 3 4 5 6 | $json_str = '{"name":"John", "age":30, "city":"New York"}' ;
$obj = json_decode( $json_str );
echo $obj ->name;
echo $obj ->age;
echo $obj ->city;
|
Copy after login
Example 2:
1 2 3 4 5 6 | $json_str = '{"name":"John", "age":30, "city":"New York"}' ;
$assoc_arr = json_decode( $json_str , true);
echo $assoc_arr [ "name" ];
echo $assoc_arr [ "age" ];
echo $assoc_arr [ "city" ];
|
Copy after login
- json_last_error function
json_last_error function is used to obtain the error code of the last JSON parsing error. Its basic syntax is as follows:
int json_last_error()
Example:
1 2 3 4 5 6 | $json_str = '{"name":"John, "age":30, "city":"New York"}' ;
$obj = json_decode( $json_str );
if (json_last_error() !== JSON_ERROR_NONE) {
echo "JSON解析错误:" . json_last_error_msg();
}
|
Copy after login
2. JSON generation function
- json_encode function
The json_encode function is used to convert PHP data into a JSON string. Its basic syntax is as follows:
string json_encode(mixed $value[, int $options = 0[, int $depth = 512]])
Parameter description:
- $value: PHP data to be converted to a JSON string
- $options: Optional parameters, specify additional encoding options, the default is 0
- $depth: Optional parameters, Specify the recursion depth limit, the default is 512
Example 1:
1 2 3 4 | $arr = array ( "name" => "John" , "age" => 30, "city" => "New York" );
$json_str = json_encode( $arr );
echo $json_str ;
|
Copy after login
Example 2:
1 2 3 4 5 6 7 8 9 10 | $arr = array ( "name" => "John" , "age" => 30, "city" => "New York" );
$json_str = json_encode( $arr , JSON_PRETTY_PRINT);
echo $json_str ;
|
Copy after login
- json_last_error function
json_last_error function is used to obtain The last JSON generated error code. Its basic syntax is as follows:
int json_last_error()
Example:
1 2 3 4 5 6 | $arr = array ( "name" => "John" , "age" => 30, "city" => "New York" );
$json_str = json_encode( $arr );
if (json_last_error() !== JSON_ERROR_NONE) {
echo "JSON生成错误:" . json_last_error_msg();
}
|
Copy after login
Conclusion:
This article introduces the basics of using JSON parsing and generation functions in PHP methods, and corresponding code examples are given. By using these functions, we can easily parse and generate JSON strings in PHP and realize data exchange with other applications.
Note:
When using JSON functions, you need to pay attention to handling possible errors. You can check the last error code through the json_last_error and json_last_error_msg functions and perform error handling as needed.
Reference:
- PHP official documentation: https://www.php.net/manual/zh/book.json.php
The above is the detailed content of How to use JSON parsing and generating functions in PHP?. For more information, please follow other related articles on the PHP Chinese website!