1

I want to retrieve json data from mysql and print it in HTML without any conversion using php framework---symfony5. But error occurs. "Cannot use object of type App\Entity\JsonData as array"

Controller -- retrieve function:

$data = $this->getDoctrine()->getRepository(JsonData::class)->findAll();
$cdata = [];
foreach ($data as $dd) {
    array_push($cdata, json_decode($dd['json-content'], true));
}

return $this->render('index.html.twig', [
    'cdata' => $cdata,
]);

I have tried json_decode but failed.

  • check [here](https://stackoverflow.com/questions/6815520/cannot-use-object-of-type-stdclass-as-array) – Kofi Jan 16 '22 at 08:55
  • I have read it, but that is not the case for me. I want to retrieve from a table that contains [column1 string] and [column2 json]. I want to use for loop to put column2 that store json data in a array and print it out. –  Jan 16 '22 at 09:00
  • 1
    The return of `findAll` is an array of objects, so it should be `$dd->getJsonContent()`, but since the backing type it's json, you'll get it already decoded. Not sure what you have in your twig file, if you deal with the array that should be enough, if you want to print it as a string you would want to use the [`json_encode`](https://twig.symfony.com/doc/3.x/filters/json_encode.html) filter in the template. – msg Jan 16 '22 at 13:46

0 Answers0