CPSC415A
Web Programming

Day 18 Notes

PHP JSON

Decode JSON

$object = json_decode('{"one":1,"two":2}');
print_r($object);
print($object->one);
$array = json_decode('[1, 2, 3]');
print_r($array);
print($array[0]);

Encode JSON

class Object {
    public $one = 1;
    public $two = 2;
}
$object = new object();
$string = json_encode($object);
print($string);
$array = new array(1, 2, 3);
$string = json_encode($array);
print($string);