例子 1<?php $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5); echo json_encode($arr); ?>
以上例程会输出:
{"a":1,"b":2,"c":3,"d":4,"e":5}例子 2对象 进行 JSON 编码
<?php class user{ public $id; public $username; public $passwd; public $group; function __construct($id,$username,$passwd,$group){ $this->id = $id; $this->username = $username; $this->passwd = $passwd; $this->group = $group; } } $newUser = new user(1,"test","123456","normal"); echo json_encode($newUser); ?>以上例程会输出:
{"id":1,"username":"test","passwd":"123456","group":"normal"}