Skip to content

PHP JsonHelper

Andrew Male edited this page Jul 27, 2015 · 2 revisions

Home » Helpers

Contents

Purpose

The JsonHelper class provides several simple shortcut methods to perform simple actions related to JSON formatted data.

Code Docs

Automatically generated documentation for JsonHelper is available here:

http://n2framework.com/phpDoc/v2.0.1/classes/N2f.JsonHelper.html

Decoding

The two methods available for decoding a JSON formatted string are little more than aliases for the appropriate calls to json_decode().

$json = "{ \"test\": \"value\" }";
$Jh = new \N2f\JsonHelper();

print_r($Jh->Decode($json));
// Outputs:
// stdClass Object
// (
//     [test] => value
// )

print_r($Jh->DecodeAssoc($json));
// Outputs:
// Array
// (
//     [test] => value
// )

Encoding

The two encoding methods are essentially calls to json_encode(), one directly and one with an attempt to make more human-readable output.

$data = array('test' => 'value');

echo($Jh->Encode($data));
// Outputs:
// {"test":"value"}

echo($Jh->EncodePretty($data));
// Outputs:
// {
//     "test": "value"
// }

PHP Links

phpDocs

Clone this wiki locally