mod_json
This module provides methods to convert from and to JSON ( JavaScript Object Notation. ) Generally you just use the PHP json_encode()
and json_decode()
methods directly in PINP. However if for some reason these functions aren't available, this module provides a backup. In addition it has one extra method: indent()
- which allows you to pretty-print a json string.
JSON::encode
and JSON::decode
accept a behaviour parameter, which can have one of the following values or a binary OR combination of them:
SERVICES_JSON_LOOSE_TYPE | Loose typing. When parsing JSON objects this creates a PHP associative array ( or hash ) instead. |
SERVICES_JSON_SUPPRESS_ERRORS | Error suppresion. Values which can't be encoded to JSON will appear as NULL instead. |
methods
(string) | JSON::encode | Encode an array to a JSON string. |
(mixed) | JSON::decode | Decode a JSON string to an array. |
(string) | JSON::indent | Indents a JSON string to a more readable format. |
JSON::encode
(string) JSON::encode( $var, $use=0 )
(mixed) | $var | The variable to convert to a JSON string. |
(int) | $use | Behavior flags, see the list at the top. |
Returns a JSON encoded string representing the input variable.
JSON::decode
(array) JSON::decode( $str, $use=0 )
(string) | $str | The JSON string to convert to a PHP variable. |
(int) | $use | Behavior flags, see the list at the top. |
Parses the JSON string and returns its PHP equivalent.
JSON::indent
(string) JSON::indent( $str, $newLine="\n", $indentStr="\t" )
(string) | $str | The variable to convert to a JSON string. |
(string) | $newLine | The string to use for each new line. |
(string) | $indentStr | The string to use to indent each level. |
Returns a formatted JSON string with one entry per line and indentation.