I have an existing Symfony 3 app. I installed FOSRestBundle, FOSUserBundle, NelmioCorsBundle. For example, I have 2 entity: User and Tariff.
The tariff has id, name, price, description, image, user (OneToMany to User) fields. The User has id, email, created, updated, some one-to-many fields, tariff (ManyToOne to Tariff).
I try to create UserRestController::getUserTariff:
/**
* @Rest\Get(path="/users/{id}/tariff", name="get_user_tariff")
*
* @param $id
* @return Response
*/
public function getUserTariff($id)
{
/** @var User $user */
$user = $this->getDoctrine()->getRepository(User::class)->find($id);
$tariff = $this->getDoctrine()->getRepository(Tariff::class)->findBy(['id' => $user->getTariff()->getId()]);
$encoders = [new JsonEncoder()];
$normalizers = [new ObjectNormalizer()];
$serializer = new Serializer($normalizers, $encoders);
$response = new JsonResponse($serializer->serialize($tariff, 'json'));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
As a result of a request I have the following JSON:
"[{\u0022id\u0022:3,\u0022name\u0022:\u0022\\u041f\\u0440\\u043e\\u0434\\u0432\\u0438\\u043d\\u0443\\u0442\\u044b\\u0439\u0022,\u0022price\u0022:7900,\u0022description\u0022:null,\u0022leadMonthLimit\u0022:3000,\u0022status\u0022:100,\u0022active\u0022:true,\u0022comingSoon\u0022:false,\u0022image\u0022:\u0022advanced.jpg\u0022,\u0022user\u0022:null,\u0022deleted\u0022:false,\u0022product\u0022:{\u0022name\u0022:\u0022\\u0410\\u0431\\u043e\\u043d\\u0435\\u043d\\u0442\\u0441\\u043a\\u0430\\u044f \\u043f\\u043b\\u0430\\u0442\\u0430 \\u043f\\u043e \\u0442\\u0430\\u0440\\u0438\\u0444\\u0443 \\u00ab\\u041f\\u0440\\u043e\\u0434\\u0432\\u0438\\u043d\\u0443\\u0442\\u044b\\u0439\\u00bb\u0022,\u0022description\u0022:\u0022\\u0414\\u0430\\u043d\\u043d\\u044b\\u0439 \\u0442\\u0430\\u0440\\u0438\\u0444 \\u043f\\u043e\\u0437\\u0432\\u043e\\u043b\\u044f\\u0435\\u0442 \\u043e\\u0431\\u0440\\u0430\\u0431\\u0430\\u0442\\u044b\\u0432\\u0430\\u0442\\u044c \\u0434\\u043e 3000 \\u043a\\u043e\\u043d\\u0442\\u0430\\u043a\\u0442\\u043e\\u0432 \\u0432 \\u043c\\u0435\\u0441\\u044f\\u0446.\u0022,\u0022price\u0022:7900,\u0022specialPrice\u0022:null,\u0022fiscalInfo\u0022:{\u0022taxSystem\u0022:null,\u0022vatSystem\u0022:null},\u0022quantity\u0022:1,\u0022photo\u0022:\u0022advanced.jpg\u0022,\u0022photoList\u0022:[\u0022advanced.jpg\u0022]},\u0022limitsDescription\u0022:\u0022\\u0414\\u0430\\u043d\\u043d\\u044b\\u0439 \\u0442\\u0430\\u0440\\u0438\\u0444 \\u043f\\u043e\\u0437\\u0432\\u043e\\u043b\\u044f\\u0435\\u0442 \\u043e\\u0431\\u0440\\u0430\\u0431\\u0430\\u0442\\u044b\\u0432\\u0430\\u0442\\u044c \\u0434\\u043e 3000 \\u043a\\u043e\\u043d\\u0442\\u0430\\u043a\\u0442\\u043e\\u0432 \\u0432 \\u043c\\u0435\\u0441\\u044f\\u0446.\u0022,\u0022ecommerce\u0022:true,\u0022modules\u0022:[\u0022tariff.modules.ecommerce\u0022],\u0022__initializer__\u0022:null,\u0022__cloner__\u0022:null,\u0022__isInitialized__\u0022:true}]"
(in the db I have Cyrillic symbols). Have can I fix it?
My config.yml:
#FOSRestBundle
fos_rest:
body_listener: true
view:
view_response_listener: force
mime_types:
json: ['application/json; charset=UTF-8', 'application/json']
serializer:
serialize_null: true
body_converter:
enabled: true
format_listener:
rules:
- { path: '^/api', priorities: ['json'], fallback_format: json, prefer_extension: true }
- { path: '^/', priorities: [ 'html', '*/*'], fallback_format: html, prefer_extension: true }
JsonResponse
will serialize your array, but it's already serialized by Serializer
. You are now serializing your already serialized object.
Use setJson()
instead:
$response = new JsonResponse();
$response->setJson($serializer->serialize($tariff, 'json'));
Take a look at \\Symfony\\Component\\HttpFoundation\\JsonResponse
to what it's doing. Then you'll see that setting Content-Type
is already done by JsonResponse::update
(called from JsonResponse::setJson
).
If you use JsonResponse($data)
or JsonResponse::setData()
, \\json_encode
is being called.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.