Detailed introduction to the use of json_decode function in php

不言
Release: 2023-04-04 15:50:02
forward
9714 people have browsed it

This article brings you a detailed introduction to the use of the json_decode function in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

We all know that the json_decode function can be used to decode JSON format strings. Our commonly used form is as follows:

<?php
$json = &#39;some_json_data&#39;;
$result = json_decode($json, true);
Copy after login

In fact, the json_decode() function has multiple parameters. Depending on the parameters passed in, the behavior of the function will be different.

Decoded data

You may have seen the json_decode() function reporting the following error:

Warning: json_decode() expects parameter 1 to be string, xxx given in
Copy after login

According to the error message, first Each parameter must be of type string. However, the first parameter can actually be of the following three types:

  • string

  • null

  • bool

Depending on the type of parameters, the return value of the function will also change.

Return value

json_decode() function defaults to returning an object if the JSON data is parsed correctly.

But after the second parameter is passed in true or the fourth parameter is passed in JSON_OBJECT_AS_ARRAY, the function will return an array instead of an object if the JSON data is parsed correctly.

Please pay attention to the bold text. The json_decode() function returns an object or array when parsing the JSON data correctly, but in some cases the function will return other data:

  • String type and is the correct JSON format data, returns an object or array after decoding

  • Starting from PHP 5.6, there are true, false and null in JSON data, if not used In lowercase format, decoding will fail and null will be returned.

  • String type 'true', 'false', 'null' will return itself (if all are lowercase)

  • Other string type parameters return null

  • true / false / null directly return null

  • Other types The data directly reports an error

  • When the third parameter recursion depth is 1, null is returned directly

So we use foreach to decode the When iterating the data, you need to pay attention to whether the decoded data is an array. If not, an error will be reported during the iteration.

You can go to the JSON official website to learn what kind of data is in JSON format.

Recursion depth

The third parameter represents recursion Depth, the depth must be greater than 0, otherwise an error will be reported. When the recursion depth is 1, the result is null, so the minimum recursion depth is 2.

Decoding options

The fourth parameter is used to set the options:

  • JSON_PRETTY_PRINT will be filled in Some blank characters are put in for easy viewing

  • JSON_UNESCAPED_SLASHES Do not encode/

  • JSON_UNESCAPED_UNICODE Do not encode unicode characters, the default is to encode them into \uXXXX

Handle errors

We can also Used in conjunction with the json_last_error function, json_last_error_msg(PHP >= 5.5) function and the @ operator:

<?php
$json = 'some_json_data';
$result = @json_decode((string)$json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
    throw new Exception(json_last_error_msg());
}
Copy after login

The above is the detailed content of Detailed introduction to the use of json_decode function in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:segmentfault.com
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!