V
V
volia72019-06-13 16:48:44
symfony
volia7, 2019-06-13 16:48:44

How to deserialize an array of json objects in entity symfony?

Hello, there is a file with json data, you need to implement this data in the entity to write it to the database.

'{ \
  "events": [ \
    {"title":"Конференция","date":"2014-11-30T12:00:00.000Z"}, \
    {"title":"День рождения","date":"2015-04-18T12:00:00.000Z"} \
  ]\
}'
data looks like this. In other words, this is an array of objects, the serializer function takes one object, but what about an array?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
user49981, 2019-06-13
@user49981

$stdObjects = json_decode(stripslashes($json)); 
foreach($stdObjects->events as $e) {
    $event = new Event();
    $event->title = $e->title;
    $event->date = $e->date;
//  $this->getDoctrine()->persist($event);
}
// $this->getDoctrine()->flush();

// class Event {
//     public $title;
//     public $date;
// }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question