Json_encode in PHP and json_decode (those years stepped pit)

A, of json_decode () to decode JSON data converted to a variable PHP

语法:json_decode ($json [,$assoc = false [, $depth = 512 [, $options = 0 ]]])

Example:

Copy the code
Copy the code
$book = array('a'=>'xiyouji','b'=>'sanguo','c'=>'shuihu','d'=>'hongloumeng');
        $json = json_encode($book);
        
        $array = json_decode($json,TRUE);
        $obj = json_decode($json);
        var_dump($array);
        var_dump($obj);
Copy the code
Copy the code

Browser print out the results as follows:

array(4) { ["a"]=> string(7) "xiyouji" ["b"]=> string(6) "sanguo" ["c"]=> string(6) "shuihu" ["d"]=> string(11) "hongloumeng" }
object(stdClass)#2 (4) { ["a"]=> string(7) "xiyouji" ["b"]=> string(6) "sanguo" ["c"]=> string(6) "shuihu" ["d"]=> string(11) "hongloumeng" } 

Two, json_encode () JSON encoding of variables,

语法: json_encode ( $value [, $options = 0 ] )

  • Example:
Copy the code
$book = array('a'=>'xiyouji','b'=>'sanguo','c'=>'shuihu','d'=>'hongloumeng');
$json = json_encode($book);
echo $json;

Browser print out the results as follows:

{"a":"xiyouji","b":"sanguo","c":"shuihu","d":"hongloumeng"};

Zero: return json (XXX, 201); // status code 201 may be transmitted to the end of the page

Guess you like

Origin www.cnblogs.com/2019gdiceboy/p/11768511.html