You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Nette\Utils\Json |api:] is a static class with useful functions you can use to work with JSON. It sanitizes errors in different PHP versions and throws exceptions on errors.
6
+
7
+
The following examples use these aliases:
8
+
9
+
/--php
10
+
use Nette\Utils\Json;
11
+
use Nette\Utils\JsonException;
12
+
\--
13
+
14
+
15
+
encode($value, $options = 0)
16
+
============================
17
+
18
+
Returns `$value` encoded into JSON. Accepts argument `Json::PRETTY` which formats JSON for better readability.
19
+
20
+
/--php
21
+
Json::encode($value); // Returns $value encoded in JSON
22
+
Json::encode($value, Json::PRETTY); // Returns formatted $value encoded in JSON
23
+
\--
24
+
25
+
Method `encode()` throws `JsonException` on error.
26
+
27
+
/--php
28
+
try {
29
+
Json::encode($value);
30
+
} catch (JsonException $e) {
31
+
// Exception handling
32
+
}
33
+
\--
34
+
35
+
36
+
decode($json, $options = 0)
37
+
===========================
38
+
39
+
Converts given JSON either in object or in array. Accepts argument `Json::FORCE_ARRAY` which forces an array instead of an object as the return value.
40
+
41
+
/--php
42
+
Json::decode('{"variable": true}'); // Returns an object of stdClass with attribute $variable
43
+
Json::decode('{"variable": true}', Json::FORCE_ARRAY); // Returns an array with key "variable" and value true
44
+
\--
45
+
46
+
Method `decode()` throws `JsonException` on error.
0 commit comments