Detailed explanation of json_decode function

json_decode is a PHP built-in function added after php5.2.0, and its function is to encode a string in JSON format. So how to use this function?

Grammar rules of json_decode:

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

json_decode accepts a string in JSON format and converts it into a PHP variable. When the parameter $assoc is TRUE, it will return an array, otherwise it will return an object.

String in JSON format
$json = '{"a":"php","b":"mysql","c":3}';

Where a is the key, and php is the key value of a.

Example:

<?php   
$json = '{"a":"php","b":"mysql","c":3}';  
$json_Class=json_decode($json);   
$json_Array=json_decode($json, true);   
print_r($json_Class);   
print_r($json_Array);         
?>

Program output:
stdClass Object (
[a] => php
[b] => mysql
[c] => 3 )
Array (
[a] => php
[b] => mysql
[c] => 3 )
in the above code On the premise of accessing the value of a of the object type $json_Class

echo $json_Class->{
    
    'a'};

Program output: php
accesses the value of a of the array type $json_Array

echo $json_Array['a'];

Program output: php

Guess you like

Origin blog.csdn.net/withkai44/article/details/131345254