File tree Expand file tree Collapse file tree 1 file changed +14
-5
lines changed Expand file tree Collapse file tree 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::
547547
548548 use Symfony\Component\HttpFoundation\JsonResponse;
549549
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
550554 $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.
554563
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 .
557566
558567.. caution ::
559568
You can’t perform that action at this time.
0 commit comments