A
A
Alexander Vladimirovich2014-02-17 10:10:38
PHP
Alexander Vladimirovich, 2014-02-17 10:10:38

What is a JSON parser in PHP?

We need a small independent PHP library for JSON parsing, which could indicate the location of the error in JSON.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Q
Quber, 2014-02-17
@Quber

https://packagist.org/packages/justinrainbow/json-...

A
Alexander Vladimirovich, 2014-03-21
@polyanin

In the example, invalid json causes an error on the line

<?php

// Get the schema and data as objects
$retriever = new JsonSchema\Uri\UriRetriever;
$schema = $retriever->retrieve('file://' . realpath('schema.json'));
$data = json_decode(file_get_contents('data.json'));

// If you use $ref or if you are unsure, resolve those references here
// This modifies the $schema object
$refResolver = new JsonSchema\RefResolver($retriever);
$refResolver->resolve($schema, 'file://' . __DIR__);

// Validate
$validator = new JsonSchema\Validator();
$validator->check($data, $schema);

if ($validator->isValid()) {
    echo "The supplied JSON validates against the schema.\n";
} else {
    echo "JSON does not validate. Violations:\n";
    foreach ($validator->getErrors() as $error) {
        echo sprintf("[%s] %s\n", $error['property'], $error['message']);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question