Home  >  Article  >  Backend Development  >  How to convert json string to array object in php

How to convert json string to array object in php

PHPz
PHPzOriginal
2023-04-25 17:35:39898browse

In PHP development, it is often necessary to convert JSON strings into arrays or objects to facilitate data processing. JSON (JavaScript Object Notation) is a lightweight data exchange format that uses syntax similar to JavaScript and can be easily parsed and generated by a variety of programming languages ​​and frameworks.

In PHP, you can use the json_decode() function to convert a JSON string into a PHP array or object. The syntax of this function is as follows:

mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )

Parameter description:

  • $json is the JSON string to be parsed.
  • $assoc is an optional parameter indicating the converted data type. If set to true, an associative array is returned; if set to false (the default), an object is returned.
  • $depth is an optional parameter indicating the maximum recursion depth. The default value is 512.
  • $options is an optional parameter indicating options when parsing JSON. The default value is 0.

Next, we demonstrate how to convert a JSON string into a PHP array or object.

Example 1: Convert JSON string to PHP array

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

print_r($assocArr);

Output result:

Array
(
    [name] => John
    [age] => 30
    [city] => New York
)

In the above example, we use the json_decode() function to convert the JSON string into Convert to associative array. It should be noted that if the $assoc parameter is set to true, an associative array is returned; otherwise, an object is returned.

Example 2: Convert JSON string to PHP object

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

echo $obj->name . "\n";
echo $obj->age . "\n";
echo $obj->city . "\n";

Output result:

John
30
New York

In the above example, we use the json_decode() function to convert the JSON string into Convert to object. It should be noted that if the $assoc parameter is not set to true (or the parameter is not passed), an object will be returned; otherwise, an associative array will be returned.

Example 3: Processing JSON strings with parsing errors

Sometimes, we will encounter some non-standard JSON strings, or the JSON string contains incorrect data. If we use the json_decode() function directly, an exception will be thrown or false will be returned. In this case, we can use a try-catch statement to handle the exception, or use the second optional parameter to detect whether the parsing was successful.

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

if ($array === null) {
    echo '解析JSON失败:' . json_last_error_msg() . "\n";
}

Output result:

解析JSON失败:Syntax error

In the above example, we deliberately deleted the ending curly braces of the JSON string, making it an illegal JSON string. By using the second optional parameter, we can detect whether the parsing was successful. If null is returned, the parsing failed. At this time, we can use the json_last_error_msg() function to obtain the specific reason for the parsing failure.

Summary

Through the above examples, we can see that converting a JSON string to an array or object in PHP is very simple, just use the json_decode() function. It should be noted that if the JSON string is not standardized, it may also cause parsing failure. We can use the try-catch statement to handle exceptions, or use the second optional parameter to detect whether the parsing is successful.

The above is the detailed content of How to convert json string to array object 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