Làm việc với json trong php

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




The json_decode() function is an inbuilt function in PHP which is used khổng lồ decode a JSON string. It converts a JSON encoded string into a PHP variable.

Bạn đang xem: Làm việc với json trong php

Bạn đã xem: Decodes a json string, how to lớn encode và decode json data in php


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

json: It holds the JSON string which need to lớn 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: php str to date


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.

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) => int(7) => int(5) => int(11) => int(19)array(4) => int(7) => int(5) => int(11) => 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:

7551119Common Errors while using json_decode() function:

Used strings are valid JavaScript but not valid JSON.Name & 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 lượt thích first_page Previous PHP | ftp_mkdir() function Next last_page PHP | json_encode() Function Recommended ArticlesPage : How lớn get the function name inside a function in PHP ?25, Sep 19PHP 5 vs PHP 705, May 16PHP | Get PHP configuration information using phpinfo()08, Jan 18PHP | php.ini tệp tin Configuration17, Jul 19How to import config.php file in a PHP script ?02, Mar 20PHP | imagecreatetruecolor() Function18, Sep 18PHP | fpassthru( ) Function11, Jun 18PHP | ImagickDraw getTextAlignment() Function16, Dec 19PHP | DsSequence last() Function24, Jan 19PHP | Imagick floodFillPaintImage() Function30, Jul 19Function khổng lồ escape regex patterns before applied in PHP31, Oct 19PHP | array_udiff_uassoc() Function11, Jul 18PHP | geoip_continent_code_by_name() Function01, Jul 18PHP | GmagickPixel setcolor() function23, Jan 20PHP | opendir() Function11, Jul 18PHP | cal_to_jd() Function30, Aug 18PHP | stream_get_transports() Function17, Dec 18PHP | DsDeque pop() Function31, Jan 19PHP | SimpleXMLElement children() Function27, Jun 19PHP | array_intersect_ukey() Function01, Jun 18PHP | Imagick adaptiveSharpenImage() Function06, Sep 18PHP | XMLWriter endDtdEntity() Function18, Mar 20PHP | isset() Function26, Apr 2004, Jun 18Article Contributed By :

Code_Mech Code_Mech Vote for difficulty Easy Normal Medium Hard Expert Improved By : Report Issue What’s NewData Structures & Algorithms – Self Paced CourseView DetailsAd-Free Experience – obatambeienwasirherbal.com.com PremiumView DetailsMost popular in PHPMost visited in website Technologies

Writing code in comment? Please use ide.obatambeienwasirherbal.com.com, generate liên kết and cốt truyện the link here.

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