json测试

    xiaoxiao2021-03-25  92

    <?php //生成json对象 $animals=array('a'=>'teager','b'=>'bear','panda','c'=>'chichen'); var_dump(json_encode($animals));//sring(51)"{"a":"teager","b":"bear","0":"panda","c":"chichen"}"  echo json_encode($animals).'<hr/>'; //结果{"a":"teager","b":"bear","c":"chichen"} $ani['ani']=$animals; var_dump(json_encode($ani)); //解析json对象 $jn_animals=json_encode($animals); var_dump(json_decode($jn_animals)); //结果string(39) "{"a":"teager","b":"bear","c":"chichen"}" echo '<hr/>'; var_dump(json_decode($jn_animals,true)); //结果array(3) { ["a"]=> string(6) "teager" ["b"]=> string(4) "bear" ["c"]=> string(7) "chichen" } echo '<hr/>'; //创建对象 class Person{     public $addr='北京';     public $height='163';     public $sex;     public function study(){         echo 'hello';     } } $tom=new Person(); echo json_encode($tom).'<hr/>'; //结果{"addr":"\u5317\u4eac","height":"163","sex":null} var_dump($tom); $per=json_encode($tom); var_dump(json_decode($per));   //object(stdClass)#2 (3) { ["addr"]=> string(6) "北京" ["height"]=> string(3) "163" ["sex"]=> NULL } echo '<hr/>'; var_dump(json_decode($per,true)); //array(3) { ["addr"]=> string(6) "北京" ["height"]=> string(3) "163" ["sex"]=> NULL }

    $test='{"a":"teager","b":"bear","c":"chichen"}';

    //注意自己创建的json对象必须使用单引号;双引号有可能不被识别

    echo'<hr/>';

    var_dump(json_decode($test,true));

    结果如图

    转载请注明原文地址: https://ju.6miu.com/read-34724.html

    最新回复(0)