Home >Backend Development >PHP Tutorial >Detailed introduction to the use of json_decode function in php

Detailed introduction to the use of json_decode function in php

不言
不言forward
2018-12-29 10:13:069965browse

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);

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

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

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!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete