The package provides methods to encode and decode JSON.
- It throws
\JsonException
even if PHP version is lower than 7.3. - It has sensible defaults so you don't have to specify flags all the time.
- It has handy methods to encode for HTML safely.
- It handles
\JsonSerializable
and\SimpleXMLElement
well.
Encoding:
use \Yiisoft\Json\Json;
$data = ['name' => 'Alex', 'team' => 'Yii'];
$json = Json::encode($data);
Encoding for HTML:
use \Yiisoft\Json\Json;
$data = ['name' => 'Alex', 'team' => 'Yii'];
$json = Json::htmlEncode($data);
Decoding:
use \Yiisoft\Json\Json;
$json = '{"name":"Alex","team":"Yii"}';
$data = Json::decode($json);