1. Go to this page and download the library: Download ybushenko/json-parser 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/ */
ybushenko / json-parser example snippets
use JsonParser\Config;
use JsonParser\JsonParser;
use JsonParser\Rules\CallableRule;
use JsonParser\Rules\DotPathRule;
use JsonParser\Loader\TextLoader;
$json = <<<JSON
{
"data": {
"general": {
"id": 12,
"category": "Eat",
"url": "/goods/category/eat"
},
"goods": [
{
"id":1,
"names": {
"ru_name":"Печенье",
"en_name":"Cookie"
},
"cost":12.5
},{
"id":2,
"names": {
"ru_name":"Молоко",
"en_name":"Milk"
},
"cost":17
},{
"id":3,
"names": {
"ru_name":"Яблоко",
"en_name":"Apple"
},
"cost":19
}
]
}
}
JSON;
$rules = [
'ru_name' => new DotPathRule('names.ru_name'),
'en_name' => new DotPathRule('names.en_name'),
'custom' => new CallableRule(function ($item) {
return $item['names']['ru_name'].$item['names']['en_name'];
})
];
$config = (new Config($rules))
->setLoader(new TextLoader($json))
->setBasePath('data.goods')
->setIgnoreErrors(true);
$result = (new JsonParser($config))->parse();
var_dump($result);