php 对象数组转json字符串5

Php代码   收藏代码
  1. <?php  
  2.     header("content-type:text/html; charset=utf-8");  
  3.     class Student {  
  4.         private $number;  
  5.         private $name;  
  6.         private $score;  
  7.         function __construct($number,$name,$score){  
  8.             $this->number=$number;  
  9.             $this->name=$name;  
  10.             $this->score=$score;  
  11.         }  
  12.         public function getNumber() {  
  13.             return $this->number;  
  14.         }  
  15.       
  16.         public function getName() {  
  17.             return $this->name;  
  18.         }  
  19.       
  20.         public function getScore() {  
  21.             return $this->score;  
  22.         }  
  23.       
  24.         public function setNumber($number) {  
  25.             $this->number = $number;  
  26.         }  
  27.       
  28.         public function setName($name) {  
  29.             $this->name = $name;  
  30.         }  
  31.       
  32.         public function setScore($score) {  
  33.             $this->score = $score;  
  34.         }  
  35.   
  36.     }  
  37.         $student1 = new Student("01","汤姆",80);  
  38.         $student2 = new Student("02","杰瑞",85);  
  39.         $arr =array();  
  40.         array_push($arr$student1);  
  41.         array_push($arr$student2);  
  42.         print_r($arr);  
  43.         $json_string = json_encode($arr,JSON_UNESCAPED_UNICODE);  
  44.         echo $json_string;   
  45.       
  46. ?>  

Php代码   收藏代码
  1. 结果为  
  2. X-Powered-By: PHP/5.5.7  
  3. Set-Cookie: ZendDebuggerCookie=127.0.0.1%3A10137%3A0||084|77742D65|1020; path=/  
  4. content-type:text/html; charset=utf-8  
  5.   
  6. Array  
  7. (  
  8.     [0] => Student Object  
  9.         (  
  10.             [number:Student:private] => 01  
  11.             [name:Student:private] => 汤姆  
  12.             [score:Student:private] => 80  
  13.         )  
  14.   
  15.     [1] => Student Object  
  16.         (  
  17.             [number:Student:private] => 02  
  18.             [name:Student:private] => 杰瑞  
  19.             [score:Student:private] => 85  
  20.         )  
  21.   
  22. )  
  23.   
  24. [{},{}]  

按官方的说法,私有变量无法被序列化出来
但是我并没有直接访问私有变量啊
如想得到以下结果,该怎么做?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值