Json

Type
Class
Namespace
craft\helpers
Inherits
craft\helpers\Json » yii\helpers\Json » yii\helpers\BaseJson
Since
3.0

Class Json

View source

Public Properties

PropertyDescription
$jsonErrorMessagesarray

Public Methods

MethodDescription
decode()Decodes the given JSON string into a PHP data structure.
decodeIfJson()Decodes the given JSON string into a PHP data structure, only if the string is valid JSON.
encode()Encodes the given value into a JSON string.
errorSummary()Generates a summary of the validation errors.
htmlEncode()Encodes the given value into a JSON string HTML-escaping entities so it is safe to be embedded in HTML code.

decodeIfJson()

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

View source

Arguments

  • $str (string) – The string to be decoded, if it's valid JSON.
  • $asArray (boolean) – Whether to return objects in terms of associative arrays.

Returns

mixed – The PHP data, or the given string if it wasn’t valid JSON.

Signature

public static mixed decodeIfJson ( \craft\helpers\string $str, \craft\helpers\bool $asArray = true )

Protected Methods

MethodDescription
handleJsonError()Handles encode() and decode() errors by throwing exceptions with the respective error message.
processData()Pre-processes the data before sending it to json_encode().