forked from nette/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.texy
97 lines (66 loc) · 2.69 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 İşlevleri
**************
.[perex]
[api:Nette\Utils\Json] JSON kodlama ve kod çözme işlevlerine sahip durağan bir sınıftır. Farklı PHP sürümlerindeki güvenlik açıklarını ele alır ve hatalarda istisnalar atar.
Kurulum:
```shell
composer require nette/utils
```
Tüm örnekler aşağıdaki sınıf takma adının tanımlandığını varsayar:
```php
use Nette\Utils\Json;
```
Kullanım .[#toc-usage]
======================
encode(mixed $value, bool $pretty=false, bool $asciiSafe=false, bool $htmlSafe=false, bool $forceObjects=false): string .[method]
---------------------------------------------------------------------------------------------------------------------------------
`$value` adresini JSON biçimine dönüştürür.
`$pretty` ayarlandığında, JSON'u daha kolay okuma ve anlaşılırlık için biçimlendirir:
```php
Json::encode($value); // JSON döndürür
Json::encode($value, pretty: true); // daha net JSON döndürür
```
`$asciiSafe` ayarlandığında, ASCII çıktısı üretir, yani unicode karakterleri `\uxxxx` dizileriyle değiştirir:
```php
Json::encode('žluťoučký', asciiSafe: true);
// '"\u017elu\u0165ou\u010dk\u00fd"'
```
`$htmlSafe` parametresi, çıktının HTML'de özel anlamı olan karakterler içermemesini sağlar:
```php
Json::encode('one<two & three', htmlSafe: true);
// '"one\u003Ctwo \u0026 three"'
```
`$forceObjects` ile, sayısal anahtarlara sahip diziler bile JavaScript nesneleri olarak kodlanacaktır:
```php
Json::encode(['a', 'b', 'c']);
// '["a","b","c"]'
Json::encode(['a', 'b', 'c'], forceObjects: true);
// '{"0":"a","1":"b","2":"c"}'
```
Hata durumunda bir `Nette\Utils\JsonException` istisnası atar.
```php
try {
$json = Json::encode($value);
} catch (Nette\Utils\JsonException $e) {
// İstisna işleme
}
```
decode(string $json, bool $forceArray=false): mixed .[method]
-------------------------------------------------------------
JSON'u PHP'ye ayrıştırır.
`$forceArray` ayarı, nesneler yerine dizilerin döndürülmesini zorlar:
```php
Json::decode('{"variable": true}'); // stdClass türünde bir nesne döndürür
Json::decode('{"variable": true}', forceArrays: true); // bir dizi döndürür
```
Hata durumunda bir `Nette\Utils\JsonException` istisnası atar.
```php
try {
$value = Json::decode($json);
} catch (Nette\Utils\JsonException $e) {
// İstisna işleme
}
```
Bir Sunucudan JSON Nasıl Gönderilir? .[#toc-how-to-send-a-json-from-a-presenter]
================================================================================
Örneğin `action*()` yönteminde çağrılabilen `$this->sendJson($data)` yöntemini kullanabilirsiniz, [Yanıt Gönderme |application:presenters#Sending a Response] bölümüne bakın.