Current Location: Home> Function Categories> json_decode

json_decode

Decode strings in JSON format
Name:json_decode
Category:JSON
Programming Language:php
One-line Description:Decode the JSON string.

Definition and usage

The json_decode() function is used to decode or convert JSON objects into PHP objects.

Example

Example 1

Store JSON data in a PHP variable and decode it into a PHP object:

 <?php
$jsonobj = '{"Bill":35,"Elon":37,"Steve":43}' ;

var_dump ( json_decode ( $jsonobj ) ) ;
?>

Run the instance

Example 2

Store the JSON data in a PHP variable and decode it into a PHP associative array:

 <?php
$jsonobj = '{"Bill":35,"Elon":37,"Steve":43}' ;

var_dump ( json_decode ( $jsonobj , true ) ) ;
?>

Run the instance

Example 3

How to access values ​​from PHP objects:

 <?php
$jsonobj = '{"Bill":35,"Elon":37,"Steve":43}' ;

$obj = json_decode ( $jsonobj ) ;

echo $obj -> Bill ;
echo $obj -> Elon ;
echo $obj -> Joe ;
?>

Run the instance

Example 4

How to access values ​​from PHP associative arrays:

 <?php
$jsonobj = '{"Bill":35,"Elon":37,"Steve":43}' ;

$arr = json_decode ( $jsonobj , true ) ;

echo $arr [ "Bill" ] ;
echo $arr [ "Elon" ] ;
echo $arr [ "Steve" ] ;
?>

Run the instance

grammar

 json_decode ( string , assoc , depth , options )
parameter describe
string Required. Specifies the value to decode.
assoc

Optional. Specifies a boolean value. The default is false.

When set to true, the returned object will be converted to an associative array.

When set to false, it returns an object.

depth Optional. Specifies the recursive depth. The default recursion depth is 512.
options Optional. Specify bitmasks (JSON_BIGINT_AS_STRING, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_OBJECT_AS_ARRAY, JSON_THROW_ON_ERROR)
Similar Functions
Popular Articles