How to convert JSON string into PHP object?

WBOY
Release: 2024-03-22 12:10:01
Original
661 people have browsed it

How to convert JSON string into PHP object?

JSON (JavaScript Object Notation) is a lightweight data exchange format commonly used for front-end and back-end data transmission. In PHP, we can convert JSON strings into PHP objects in order to process and operate the data. Next, let's look at specific code examples.

First, suppose we have a JSON string as shown below:

{
    "name": "John Doe",
    "age": 30,
    "city": "New York"
}
Copy after login

Next, we will use PHP’s built-in function json_decode() to convert this JSON string Convert to PHP object. The code is as follows:

$jsonString = '{"name": "John Doe", "age": 30, "city": "New York"}';
$phpObject = json_decode($jsonString);

// 检查转化后的PHP对象
var_dump($phpObject);
Copy after login

In the above code, the json_decode() function is used to convert the JSON string $jsonString into a PHP object $phpObject. We can use the var_dump() function to view the converted PHP object. After executing the above code, we will get the following output:

object(stdClass)#1 (3) {
  ["name"] => string(8) "John Doe"
  ["age"] => int(30)
  ["city"] => string(8) "New York"
}
Copy after login

As can be seen from the output, the JSON string has been successfully converted into a PHP object, and we can access and manipulate data through the properties of the object. For example, we can use the following code to get the attribute value in the object:

echo $phpObject->name;  // 输出:John Doe
echo $phpObject->age;   // 输出:30
echo $phpObject->city;  // 输出:New York
Copy after login

It should be noted that the json_decode() function also has an optional parameter $assoc , the default is false. When this parameter is set to true, the JSON string will be converted into an associative array instead of an object. An example is as follows:

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

// 检查转化后的PHP关联数组
var_dump($phpArray);
Copy after login

Through the above code example, we can clearly understand how to convert a JSON string into a PHP object, and how to access and manipulate the data through attributes or indexes. Conversion between JSON and PHP is a common operation in development, which can better realize data transmission and processing.

The above is the detailed content of How to convert JSON string into PHP object?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!