我的POST动作看起来像这样:
public function cpostAction(Request $request)
{
$records = json_decode($request->getContent(), true);
$arr = array();
$em = $this->getDoctrine()->getManager();
foreach((array)$records as $record) {
$obj = new \stdClass();
$entity = new Record();
$entity->setRouteId($record['routeId']);
$entity->setDate(new \DateTime('now'));
$entity->setUserId($record['userId']);
$entity->setSync($record['sync']);
$obj->id = $record['id'];
$em->persist($entity);
$em->flush();
$obj->remote_id = $entity->getId();
$obj = json_encode($obj, true);
array_push($arr, $obj);
}
$view = View::create();
$view->setFormat('json');
$view->setData($arr);
return $this->get('fos_rest.view_handler')->handle($view);
}
如果我保留$obj = json_encode($obj, true);
,我会收到此回复
[
"{\"id\":1,\"remote_id\":52}",
"{\"id\":2,\"remote_id\":53}",
"{\"id\":3,\"remote_id\":54}"
]
如果没有这行代码,我会得到
{
"0": {},
"1": {},
"2": {}
}
如何获得正确的JSON? 感谢您的任何建议!