Tutorials Algorithms Analysis of Algorithms Data Structures Languages Interview Corner GATE ISRO CS UGC NET CS CS Subjects Web Technologies Student Jobs

*

*

*

*

The json_decode() function is an inbuilt function in PHP which is used to decode a JSON string. It converts a JSON encoded string into a PHP variable.

Đang xem: Decodes a json string

Syntax:

json_decode( $json, $assoc = FALSE, $depth = 512, $options = 0 )Parameters: This function accepts four parameters as mentioned above and described below:

json: It holds the JSON string which need to be decode. It only works with UTF-8 encoded strings.assoc: It is a boolean variable. If it is true then objects returned will be converted into associative arrays. depth: It states the recursion depth specified by user.

Xem thêm: Cung Hoàng Đạo Taurus Ý Tưởng, Tính Cách Cung Kim Ngưu (Taurus)

options: It includes bitmask of JSON_OBJECT_AS_ARRAY, JSON_BIGINT_AS_STRING,, JSON_THROW_ON_ERROR.

Return values: This function returns the encoded JSON value in appropriate PHP type. If the json cannot be decoded or if the encoded data is deeper than the recursion limit then it returns NULL.

Xem thêm: Phần Mềm Oy Sing ➡ App Store Review ✅ Aso, Oy Sing ➡ App Store Review ✅ Aso

Below examples illustrate the use of json_decode() function in PHP:Example 1:

// Declare a json string
$json = “{“g”:7, “e”:5, “e”:5, “k”:11, “s”:19}”;
// Use json_decode() function to
// decode a string
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>
Output:

object(stdClass)#1 (4) { <"g">=> int(7) <"e">=> int(5) <"k">=> int(11) <"s">=> int(19)}array(4) { <"g">=> int(7) <"e">=> int(5) <"k">=> int(11) <"s">=> int(19)}
Example 2:

// Declare a json string
$json = “{“geeks”: 7551119}”;
// Use json_decode() function to
// decode a string
$obj = json_decode($json);
// Display the value of json object
print $obj->{“geeks”};
?>
Output:

7551119
Common Errors while using json_decode() function:

Used strings are valid JavaScript but not valid JSON.Name and value must be enclosed in double quotes, single quotes are not allowed.Trailing commas are not allowed.

Reference: http://php.net/manual/en/function.json-decode.php

My Personal Notes arrow_drop_up
Save
favorite_border Like
first_page Previous
PHP | ftp_mkdir() function
Next last_page
PHP | json_encode() Function
Recommended Articles
Page :
How to get the function name inside a function in PHP ?
25, Sep 19
PHP 5 vs PHP 7
05, May 16
PHP | Get PHP configuration information using phpinfo()
08, Jan 18
PHP | php.ini File Configuration
17, Jul 19
How to import config.php file in a PHP script ?
02, Mar 20
PHP | imagecreatetruecolor() Function
18, Sep 18
PHP | fpassthru( ) Function
11, Jun 18
PHP | ImagickDraw getTextAlignment() Function
16, Dec 19
PHP | DsSequence last() Function
24, Jan 19
PHP | Imagick floodFillPaintImage() Function
30, Jul 19
Function to escape regex patterns before applied in PHP
31, Oct 19
PHP | array_udiff_uassoc() Function
11, Jul 18
PHP | geoip_continent_code_by_name() Function
01, Jul 18
PHP | GmagickPixel setcolor() function
23, Jan 20
PHP | opendir() Function
11, Jul 18
PHP | cal_to_jd() Function
30, Aug 18
PHP | stream_get_transports() Function
17, Dec 18
PHP | DsDeque pop() Function
31, Jan 19
PHP | SimpleXMLElement children() Function
27, Jun 19
PHP | array_intersect_ukey() Function
01, Jun 18
PHP | Imagick adaptiveSharpenImage() Function
06, Sep 18
PHP | XMLWriter endDtdEntity() Function
18, Mar 20
PHP | isset() Function
26, Apr 20
04, Jun 18
Article Contributed By :

*

Code_Mech
Code_Mech
Vote for difficulty
Easy Normal Medium Hard Expert
Improved By :
Report Issue
What’s New
Data Structures and Algorithms – Self Paced Course
View Details
Ad-Free Experience – darkedeneurope.com Premium
View Details
Most popular in PHP
Most visited in Web Technologies

Writing code in comment? Please use ide.darkedeneurope.com, generate link and share the link here.

Load Comments
5th Floor, A-118, Sector-136, Noida, Uttar Pradesh – 201305
feedback
darkedeneurope.com
Company Learn Practice Contribute
darkedeneurope.com , Some rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy Got It !

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *