Home>Article>Backend Development> Quickly decode specified JSON string via PHP

Quickly decode specified JSON string via PHP

藏色散人
藏色散人 Original
2021-08-03 11:30:58 2884browse

In the previous article, I introduced to you how to use an unordered list to display the values in a PHP array. Friends who are interested can click ->《Teach you to use an unordered list to display the values Values in PHP array》.

This article introduces you to a new little knowledge, which is to decode the specified JSON string through PHP code.

First look at the JSON sample code, as follows:

{"Title": "The Cuckoos Calling", "Author": "Robert Galbraith", "Detail": { "Publisher": "Little Brown" }}

The question is how to decode this JSON code?

Very simple.

The PHP code is as follows:

"; } $a = '{"Title": "The Cuckoos Calling", "Author": "Robert Galbraith", "Detail": { "Publisher": "Little Brown" } }'; $j1 = json_decode($a,true); array_walk_recursive($j1,"w3rfunction");

The running results are as follows:

Quickly decode specified JSON string via PHP

Here we must introduce to you a key function json_decode();

json_decodeThe function is to decode the string in JSON format.

The syntax is:

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

Can accept a JSON encoded string and convert it into a PHP variable. The return value is returned in json through the appropriate PHP type. data. The values true, false and null will return true, false and null accordingly. If json cannot be decoded, or the depth of the encoded data exceeds the recursion limit, null will be returned.

The parameters respectively represent:

json: a string in json string format to be decoded. This function can only process UTF-8 encoded data.

assoc: When this parameter is true, array will be returned instead of object.

depth: Specify the recursion depth.

options: Mask consisting of JSON_BIGINT_AS_STRING, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_OBJECT_AS_ARRAY, JSON_THROW_ON_ERROR.

Now that the introduction is here, let me tell you about thejson_encodefunction. This function is used to JSON encode variables, which is exactly the opposite of json_decode. For those who are interested You can read the article "Detailed explanation of json_encode() and json_decode() functions in php".

Finally, I would like to recommend to you the latest free course on our platform "Entering the World of PHP from 0"~ Come and learn!

The above is the detailed content of Quickly decode specified JSON string via 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