1. Go to this page and download the library: Download kestrelwp/php-jsonc-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/ */
use Kestrel\JsoncParser\JsoncParser;
$json = '{"x": 1, "y": 2}';
$errors = [];
$tree = JsoncParser::parseTree($json, $errors);
// Navigate the tree
foreach ($tree->children as $property) {
$key = $property->children[0]->value; // Property name
$value = $property->children[1]->value; // Property value
echo "$key = $value\n";
}
use Kestrel\JsoncParser\JsoncParser;
use Kestrel\JsoncParser\Parser\JsonVisitor;
$visitor = new class implements JsonVisitor {
public function onObjectBegin(int $offset, int $length, int $startLine, int $startCharacter, \Closure $pathSupplier): bool|null {
echo "Object started at offset $offset\n";
return null;
}
public function onObjectProperty(string $property, int $offset, int $length, int $startLine, int $startCharacter, \Closure $pathSupplier): bool|null {
echo "Property: $property\n";
return null;
}
public function onLiteralValue(mixed $value, int $offset, int $length, int $startLine, int $startCharacter, \Closure $pathSupplier): bool|null {
echo "Value: $value\n";
return null;
}
// Implement other interface methods...
public function onObjectEnd(int $offset, int $length, int $startLine, int $startCharacter): bool|null { return null; }
public function onArrayBegin(int $offset, int $length, int $startLine, int $startCharacter, \Closure $pathSupplier): bool|null { return null; }
public function onArrayEnd(int $offset, int $length, int $startLine, int $startCharacter): bool|null { return null; }
public function onSeparator(string $character, int $offset, int $length, int $startLine, int $startCharacter): bool|null { return null; }
public function onComment(int $offset, int $length, int $startLine, int $startCharacter): bool|null { return null; }
public function onError(int $error, int $offset, int $length, int $startLine, int $startCharacter): bool|null { return null; }
};
$json = '{"name": "Bob", "age": 30}';
JsoncParser::visit($json, $visitor);