File tree 1 file changed +14
-5
lines changed
1 file changed +14
-5
lines changed Original file line number Diff line number Diff line change @@ -547,13 +547,22 @@ class, which can make this even easier::
547
547
548
548
use Symfony\Component\HttpFoundation\JsonResponse;
549
549
550
+ // if you know the data to send when creating the response
551
+ $response = new JsonResponse(array('data' => 123));
552
+
553
+ // if you don't know the data to send when creating the response
550
554
$response = new JsonResponse();
551
- $response->setData(array(
552
- 'data' => 123
553
- ));
555
+ // ...
556
+ $response->setData(array('data' => 123));
557
+
558
+ // if the data to send is already encoded in JSON
559
+ $response = JsonResponse::fromJsonString('{ "data": 123 }');
560
+
561
+ .. versionadded :: 3.2
562
+ The ``JsonResponse::fromJsonString() `` method was added in Symfony 3.2.
554
563
555
- This encodes your array of data to JSON and sets the ``Content-Type `` header
556
- to ``application/json ``.
564
+ The `` JsonResponse `` class sets the ``Content-Type `` header to
565
+ ``application/json `` and encodes your data to JSON when needed .
557
566
558
567
.. caution ::
559
568
You can’t perform that action at this time.
0 commit comments