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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With