PHP code example of masiuchi / inside-json
1. Go to this page and download the library: Download masiuchi/inside-json library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
masiuchi / inside-json example snippets
use InsideJson\Decoder;
use InsideJson\Encoder;
$decoder = new Decoder;
$encoder = new Encoder;
# JSON string has serialized JSON string its inside.
$json = '{"a":"{\"b\":1,\"c\":2}","d":3}';
# decode JSON and inside JSON at once
$obj = $decoder->decode($json);
# access decoded object by using array-access or property
$obj['a']['b'] = 4;
$obj->a->c = 5;
# access decode object by using foreach
foreach ($obj as $key1 => $value1) {
if (is_object($obj)) {
echo "$key1:\n";
foreach ($value1 as $key2 => $value2) {
echo "\t$key2: $value2\n";
}
} else {
echo "$key1: $value1\n";
}
}
# encode to JSON string keeping inside JSON
$encoder->encode($obj); # '{"a":"{\"b\":4,\"c\":5}","d":3}'
# encode to JSON string expanding inside JSON
json_encode($obj->toArray()); # '{"a":{"b":4,"c":5},"d":3}'