In JSON PHP the method of deserializing an Json string into an array of objects and

  • 2021-10-13 06:52:10
  • OfStack

As shown below:


<?php
//php Inverse coding parsing json Information 
//json_decode(json String );
$city = array('shandong'=>'jinan','henan'=>'zhengzhou','hebei'=>'shijiazhuang');
$jn_city = json_encode($city);
// Inverse coding json
$fan_city = json_decode($jn_city,false);// No. 1 2 Parameters false Returns the object Type, false You can not write by default 
var_dump($fan_city);//object(stdClass)#1 (3) { ["shandong"]=> string(5) "jinan" ["henan"]=> string(9) "zhengzhou" ["hebei"]=> string(12) "shijiazhuang" } 
echo "<br />";
$fan_city = json_decode($jn_city,true);// No. 1 2 Parameters true Returns the array Type 
var_dump($fan_city);//array(3) { ["shandong"]=> string(5) "jinan" ["henan"]=> string(9) "zhengzhou" ["hebei"]=> string(12) "shijiazhuang" }

Manually written JSON String 1 must be in single quotation marks to successfully deserialize into an object/array:


<?php 
 
//json Inverse coding of information  
 
// Different php Version, for "pure json There is a problem with the parsing of string  
// Defined in double quotation marks json The string de-encoding operation becomes null 
//$jn = "{'name':'tom','age':'20','addr':'beijing'}"; 
//$fan_jn = json_decode($jn,true); 
//var_dump($fan_jn);//NULL 
 
// Defined using single quotation marks json The string de-encoding operation will succeed  
$jn = '{"name":"tom","age":"20","addr":"beijing"}'; 
$fan_jn = json_decode($jn,true); 
var_dump($fan_jn); 

Related articles: