-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path01-decoding-json.php
More file actions
46 lines (42 loc) · 820 Bytes
/
01-decoding-json.php
File metadata and controls
46 lines (42 loc) · 820 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
declare(strict_types=1);
use Suin\Json;
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(Json::decode($json));
var_dump(Json::decode($json, true));
// Output:
// object(stdClass)#%d (5) {
// ["a"]=>
// int(1)
// ["b"]=>
// int(2)
// ["c"]=>
// int(3)
// ["d"]=>
// int(4)
// ["e"]=>
// int(5)
// }
// array(5) {
// ["a"]=>
// int(1)
// ["b"]=>
// int(2)
// ["c"]=>
// int(3)
// ["d"]=>
// int(4)
// ["e"]=>
// int(5)
// }
// Error handling example
$json = "{'Organization': 'PHP Documentation Team'}";
try {
Json::decode($json);
} catch (Json\DecodingException $e) {
var_dump($e->getMessage());
var_dump($e->getContext()->json());
}
// Output:
// string(35) "Failed to decode JSON: Syntax error"
// string(42) "{'Organization': 'PHP Documentation Team'}"