Home >Backend Development >PHP Tutorial >How to use JSON parsing and generating functions in PHP?

How to use JSON parsing and generating functions in PHP?

王林
王林Original
2023-07-26 11:27:15959browse

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

  1. 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:

$json_str = '{"name":"John", "age":30, "city":"New York"}';
$obj = json_decode($json_str);

echo $obj->name;  // 输出:John
echo $obj->age;   // 输出:30
echo $obj->city;  // 输出:New York

Example 2:

$json_str = '{"name":"John", "age":30, "city":"New York"}';
$assoc_arr = json_decode($json_str, true);

echo $assoc_arr["name"];  // 输出:John
echo $assoc_arr["age"];   // 输出:30
echo $assoc_arr["city"];  // 输出:New York
  1. 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:

$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();
}

2. JSON generation function

  1. 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:

$arr = array("name" => "John", "age" => 30, "city" => "New York");
$json_str = json_encode($arr);

echo $json_str;  // 输出:{"name":"John", "age":30, "city":"New York"}

Example 2:

$arr = array("name" => "John", "age" => 30, "city" => "New York");
$json_str = json_encode($arr, JSON_PRETTY_PRINT);

echo $json_str;  
// 输出:
// {
//    "name": "John",
//    "age": 30,
//    "city": "New York"
// }
  1. 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:

$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();
}

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!

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