FOS Rest Bundle 未正确返回 json
FOS Rest Bundle not returning proper json
如您所见,'cover' 和 'parking' 索引都是数组。但是当我 return 响应时,我得到这样的结果。
'cover' 和 'parking' 键都没有在 json 响应中显示值。
所以这是我的控制器:
public function getPageInfoAction($page)
{
$request = (new FacebookRequest(
$this->initAppAction(),
'GET',
"/{$page}"
))->execute();
$graphObject = $request->getGraphObject();
//dump($graphObject);
//die;
return $graphObject->asArray();
}
这是我的 config.yml 文件
#fos_rest
fos_rest:
param_fetcher_listener: force
view:
view_response_listener: 'force'
formats:
xml: true
json: true
templating_formats:
html: true
format_listener:
rules:
- { path: ^/, priorities: [ html, json, xml ], fallback_format: ~, prefer_extension: true }
exception:
codes:
'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
messages:
'Symfony\Component\Routing\Exception\ResourceNotFoundException': true
allowed_methods_listener: true
access_denied_listener:
json: true
body_listener: true
disable_csrf_role: ROLE_API
那么我怎样才能 return 正确 json 响应?
好的,我找到了答案。
由于我的数组也有一个对象作为值,因此序列化程序在返回对 json 的响应时返回空 json 对象。所以为了解决这个问题,我检查了键是否是对象
foreach($options as $t){
if(isset($graphObject[$t]))
//
$pageInfo[$t] = gettype($graphObject[$t])== 'object'?(array)$graphObject[$t]:$graphObject[$t];
}
如您所见,'cover' 和 'parking' 索引都是数组。但是当我 return 响应时,我得到这样的结果。
'cover' 和 'parking' 键都没有在 json 响应中显示值。 所以这是我的控制器:
public function getPageInfoAction($page)
{
$request = (new FacebookRequest(
$this->initAppAction(),
'GET',
"/{$page}"
))->execute();
$graphObject = $request->getGraphObject();
//dump($graphObject);
//die;
return $graphObject->asArray();
}
这是我的 config.yml 文件
#fos_rest
fos_rest:
param_fetcher_listener: force
view:
view_response_listener: 'force'
formats:
xml: true
json: true
templating_formats:
html: true
format_listener:
rules:
- { path: ^/, priorities: [ html, json, xml ], fallback_format: ~, prefer_extension: true }
exception:
codes:
'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
messages:
'Symfony\Component\Routing\Exception\ResourceNotFoundException': true
allowed_methods_listener: true
access_denied_listener:
json: true
body_listener: true
disable_csrf_role: ROLE_API
那么我怎样才能 return 正确 json 响应?
好的,我找到了答案。 由于我的数组也有一个对象作为值,因此序列化程序在返回对 json 的响应时返回空 json 对象。所以为了解决这个问题,我检查了键是否是对象
foreach($options as $t){
if(isset($graphObject[$t]))
//
$pageInfo[$t] = gettype($graphObject[$t])== 'object'?(array)$graphObject[$t]:$graphObject[$t];
}