Craft 3 Class Reference

Class craft\helpers\Json

Inheritance
craft\helpers\Json » yii\helpers\Json » yii\helpers\BaseJson
Available since version
3.0
Source Code
https://github.com/craftcms/cms/blob/master/src/helpers/Json.php

Class Json

Public Properties
Property Type Description Defined By
$jsonErrorMessages array yii\helpers\BaseJson
Public Methods
Method Description Defined By
decode() Decodes the given JSON string into a PHP data structure. yii\helpers\BaseJson
decodeIfJson() Decodes the given JSON string into a PHP data structure, only if the string is valid JSON. craft\helpers\Json
encode() Encodes the given value into a JSON string. yii\helpers\BaseJson
errorSummary() Generates a summary of the validation errors. yii\helpers\BaseJson
htmlEncode() Encodes the given value into a JSON string HTML-escaping entities so it is safe to be embedded in HTML code. yii\helpers\BaseJson
Protected Methods
Method Description Defined By
handleJsonError() Handles encode() and decode() errors by throwing exceptions with the respective error message. yii\helpers\BaseJson
processData() Pre-processes the data before sending it to json_encode(). yii\helpers\BaseJson

Method Details

decodeIfJson() public static method #

Decodes the given JSON string into a PHP data structure, only if the string is valid JSON.

public static mixed decodeIfJson ( \craft\helpers\string $str, \craft\helpers\bool $asArray true )
$str string The string to be decoded, if it's valid JSON.
$asArray boolean Whether to return objects in terms of associative arrays.
return mixed The PHP data, or the given string if it wasn’t valid JSON.