json_encode与json_decode的应用


直接上代码:

//json_decode ( string $json [, bool $assoc ] )
//当该参数为 TRUE 时,将返回 array 而非 object 。
$a = array("name"=>"jjaa","age"=>21,"birth"=>"2012-08-04");
$json_a = json_encode($a);       //json序列化
如:
{"name":"jjaa","age":21,"birth":"2012-08-04"}

$b = json_decode($json_a);       //获得的结果是个object;
如:
stdClass Object
(
    [name] => jjaa
    [age] => 21
    [birth] => 2012-08-04
)

$b->name     //变量访问

$c = json_decode($json_a,true);    //返回的是数组

在反序列化时注意:$assoc为true,返回数组


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM