$data = array('name' => 'John', 'age' => 25, 'gender' => 'male'); $json = CJSON::encode($data); echo $json;
{"name":"John","age":25,"gender":"male"}
class Person { public $name; public $age; public $gender; public function __construct($name, $age, $gender) { $this->name = $name; $this->age = $age; $this->gender = $gender; } } $person = new Person('John', 25, 'male'); $json = CJSON::encode($person); echo $json;
{"name":"John","age":25,"gender":"male"}The CJSON library is part of the Yii framework. To use it, you need to install the Yii framework on your server.