Return JSON array as resut - Provide Json Interface | Drupal 8

Return JSON array as resut - Provide Json Interface

Submitted by editor on Fri, 11/20/2015 - 09:47
Question

How to provide json array as result of service or as output page.

You can do that using Symfony JsonResponse.

Exemple 1.
return new \Symfony\Component\HttpFoundation\JsonResponse($array);

Exemple 2.
use Symfony\Component\HttpFoundation\JsonResponse;
  public function get_result() {
    $output = array();
    $output[] = array('time' => date("Y-m-d H:i:s"));
    $output[] = array('result'=> "OK");
    return new JsonResponse($output);
  }

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.