I am trying to read the data that is received via php
and I need to read those values separately. The return is a json
object, when I show in the console using the command console.log(data)
and the following structure appears:
{"clientes":[
{"idEntidade":"314","nmEntidade":"3D Inform\u00e1tica Ltda"},
{"idEntidade":"439","nmEntidade":"Academia BigBone Fitness"},
{"idEntidade":"308","nmEntidade":"Academia de Ginastica Forca e Saude Ltda - ME"},
{"idEntidade":"371","nmEntidade":"Academia Simetria"}
]}
But if I try to access a position with the command console.log(data[0]['nmEntidade'])
for example, the answer is undefined
.
Jquery Code
$.getJSON('../caminho/do/json', function (data) {
console.log(data);
console.log(data[0]['nmEntidade']);
});
PHP method that returns the data. (I am using the framework Symfony )
public function filtraClienteAction()
{
$em = $this->getDoctrine()->getManager();
$connection = $em->getConnection();
$statement = $connection->prepare("SELECT idEntidade, nmEntidade FROM entidades ORDER BY nmEntidade");
$statement->execute();
$clientes = $statement->fetchAll();
$response = new JsonResponse(json_encode(array(
'clientes' => $clientes
)));
$response->headers->set('Content-Type', 'application/json');
return $response;
}