How to check if json is an object

0

Good gets a json on my page like this:

// Decodifica o Json
$obj = json_decode(file_get_contents('php://input'));

echo $obj->Autenticacao[0]->login;

The Json that is being sent is like this:

{
"Autenticacao": [{
    "login": "root",
    "senha": "123"
}]
}

Everything works 100%, but who does remove the json [], php informs me the following error:

Fatal error: Cannot use object of type stdClass as array

How to identify if the json is complete.

    
asked by anonymous 12.08.2017 / 13:25

1 answer

2

So if you do not want an array in the Authentication property, No you can do this:

echo $obj->Autenticacao[0]->login;

Because you are trying to access in the form of array that does not exist should be as follows:

echo $obj->Autenticacao->login;

And to access this way your json should be something like:

{
    "Autenticacao": {
        "login": "root",
        "senha": "123"
    }
}
    
12.08.2017 / 14:11