Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

\u0022 instead of " " with JsonResponse symfony

Tags:

symfony

my problem is : The data returned have \u0022 instead of "".

$em=$this->getDoctrine()->getManager();
$result = $em->getRepository('HomeBundle:Product')->findAll();

$encoders = array(new XmlEncoder(), new JsonEncoder());
$normalizers = array(new ObjectNormalizer());
$serializer = new Serializer($normalizers, $encoders);

$jsonContent = $serializer->serialize($result,'json');

$response = new JsonResponse($jsonContent);
$response->headers->set('Content-Type', 'application/json');
return $response;

and that's what i get

"[{\u0022id\u0022:1,\u0022name\u0022:\u0022opp\u0022,\u0022price\u0022:3},{\u0022id\u0022:2,\u0022name\u0022:\u0022opp\u0022,\u0022price\u0022:5},{\u0022id\u0022:3,\u0022name\u0022:\u0022oppv\u0022,\u0022price\u0022:16},{\u0022id\u0022:4,\u0022name\u0022:\u0022opp\u0022,\u0022price\u0022:6},{\u0022id\u0022:5,\u0022name\u0022:\u0022opp\u0022,\u0022price\u0022:7},{\u0022id\u0022:6,\u0022name\u0022:\u0022opp\u0022,\u0022price\u0022:34},{\u0022id\u0022:7,\u0022name\u0022:\u0022opp\u0022,\u0022price\u0022:56},{\u0022id\u0022:8,\u0022name\u0022:\u0022opp\u0022,\u0022price\u0022:30}]"

thanks in advance for your help

like image 951
mino Avatar asked Mar 13 '16 17:03

mino


1 Answers

As your entities are already serialised, change your JsonResponse to a Response :

use Symfony\Component\HttpFoundation\Response;

// ...
$response = new Response($jsonContent);
$response->headers->set('Content-Type', 'application/json');

return $response;

Or decode your results before create your JsonResponse :

return new JsonResponse(json_decode($jsonContent));

Note that the Content-Type of a JsonResponse is automatically application/json, no need to set it.

like image 77
chalasr Avatar answered Oct 18 '22 19:10

chalasr