B
B
BonBon Slick2017-07-10 16:54:30
PHP
BonBon Slick, 2017-07-10 16:54:30

json string validation?

You need to check if the incoming element is a json string.

public static function isJson($string)
    {
        json_decode($string);
        if (json_last_error() === JSON_ERROR_NONE) {
            return true;
        }
        return false;
// или
       json_decode($string);
        return (json_last_error() == JSON_ERROR_NONE);
    }
// и еще так пробовал
json_decode(123) // будет true

For digits, they return true, so how then is it safe and secure to validate the json string validation?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DevMan, 2017-07-13
@BonBonSlick

when will you learn to read the documentation?
open php.net/manual/ru/function.json-decode.php and see

Note:
PHP implements a superset of JSON, which is described in the original » RFC 7159.

follow the link to the rfc and read
JSON can represent four primitive types (strings, numbers, booleans, and null)
and two structured types (objects and arrays) .

Well, what are you going to validate if, according to the implemented standard, a number/string is a valid json?
if you really want to check that an array / object is coming in json, then use something like ideone.com/DpA6eE

A
Alexander Pushkarev, 2017-07-10
@AXP-dev

There are a lot of correct solutions here https://stackoverflow.com/questions/6041741/fastes...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question