-
-
Notifications
You must be signed in to change notification settings - Fork 282
/
json.texy
97 lines (66 loc) · 2.79 KB
/
json.texy
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
JSON funkciók
*************
.[perex]
[api:Nette\Utils\Json] egy statikus osztály JSON kódolási és dekódolási funkciókkal. Kezeli a különböző PHP verziók sebezhetőségeit és hiba esetén kivételeket dob.
Telepítés:
```shell
composer require nette/utils
```
Minden példa feltételezi, hogy a következő osztály alias van definiálva:
```php
use Nette\Utils\Json;
```
Használat .[#toc-usage]
=======================
encode(mixed $value, bool $pretty=false, bool $asciiSafe=false, bool $htmlSafe=false, bool $forceObjects=false): string .[method]
---------------------------------------------------------------------------------------------------------------------------------
Átalakítja a `$value` oldalt JSON formátumra.
A `$pretty` beállítása esetén a JSON formátumot formázza a könnyebb olvashatóság és áttekinthetőség érdekében:
```php
Json::encode($value); // JSON-t ad vissza
Json::encode($value, pretty: true); // tisztább JSON-t ad vissza
```
A `$asciiSafe` beállítása esetén ASCII kimenetet generál, azaz az unicode karaktereket `\uxxxx` szekvenciákkal helyettesíti:
```php
Json::encode('žluťoučký', asciiSafe: true);
// '"\u017elu\u0165ou\u010dk\u00fd"'
```
A `$htmlSafe` paraméter biztosítja, hogy a kimenet ne tartalmazzon a HTML-ben speciális jelentéssel bíró karaktereket:
```php
Json::encode('one<two & three', htmlSafe: true);
// '"one\u003Ctwo \u0026 three"'
```
A `$forceObjects` paraméterrel még a numerikus kulcsokkal rendelkező tömbök is JavaScript-objektumként lesznek kódolva:
```php
Json::encode(['a', 'b', 'c']);
// '["a","b","c"]'
Json::encode(['a', 'b', 'c'], forceObjects: true);
// '{"0":"a","1":"b","2":"c"}'
```
`Nette\Utils\JsonException` kivételt dob hiba esetén.
```php
try {
$json = Json::encode($value);
} catch (Nette\Utils\JsonException $e) {
// Kivételkezelés
}
```
decode(string $json, bool $forceArray=false): mixed .[method]
-------------------------------------------------------------
JSON elemzést végez PHP-ra.
A `$forceArray` beállítása objektumok helyett tömbök visszaküldését kényszeríti ki:
```php
Json::decode('{"variable": true}'); // egy stdClass típusú objektumot ad vissza.
Json::decode('{"variable": true}', forceArray: true); // egy tömböt ad vissza.
```
`Nette\Utils\JsonException` kivételt dob hiba esetén.
```php
try {
$value = Json::decode($json);
} catch (Nette\Utils\JsonException $e) {
// Kivételkezelés
}
```
Hogyan lehet JSON-t küldeni egy prezenterből? .[#toc-how-to-send-a-json-from-a-presenter]
=========================================================================================
Használhatja a `$this->sendJson($data)` metódust, amelyet például a `action*()` metódusban hívhat meg, lásd [Válasz küldése |application:presenters#Sending a Response].