2
Good need to do an error handling while retrieving a field from a json.
I have the following Json:
{
"Autenticacao": {
"login": "123",
"senha": "123"
}
}
I’m retrieving the data like this:
// Decodifica o Json
$obj = json_decode($json);
$obj->autenticacao->login;
So far so good, the problem is when the user informs a json that contains an array, like this:
{
"Autenticacao": [{
"login": "123",
"senha": "123"
}]
}
With that php gives the following error:
Notice: Trying to get property of non-object in
That occurs on the line:
$obj->autenticacao->login;
How can I treat this error and tell the user that json is not correct.
You should have it constant, or you get it one way or the other... Never with that unpredictability. Or is there some reason for it?
– Miguel
This is counting, but if someone from the Front-end forget is sending wrong I want to treat the error, avoiding the error in php and the creation of a log.
– Hugo Borges