TYPO3 Extbase-操纵/更改返回的json
如何操作/更改从以下位置返回的json:TYPO3 Extbase-操纵/更改返回的json,json,typo3,extbase,typo3-7.6.x,Json,Typo3,Extbase,Typo3 7.6.x,如何操作/更改从以下位置返回的json: [{ "name": "Audi", "owner": "Peter", "price": 0, "color": "Blue", "pid": 0, "uid": 1 }, { "name": "BMW", "owner": "Wolfgang", "price": 0, "color": "Black", "pid": 0, "uid": 2 }] 例如
[{
"name": "Audi",
"owner": "Peter",
"price": 0,
"color": "Blue",
"pid": 0,
"uid": 1
}, {
"name": "BMW",
"owner": "Wolfgang",
"price": 0,
"color": "Black",
"pid": 0,
"uid": 2
}]
例如:
{
"data": [{
"DT_RowId": "row_1",
"name": "Audi",
"owner": "Peter"
}, {
"DT_RowId": "row_2",
"name": "BMW",
"owner": "Wolfgang"
}],
"options": [],
"files": [],
"draw": 1,
"recordsTotal": "2",
"recordsFiltered": "16"
}
我在我的控制器中尝试了这一点,但它甚至没有过滤名称和所有者:
/**
* @var string
*/
protected $defaultViewObjectName = 'TYPO3\CMS\Extbase\Mvc\View\JsonView';
public function jsonRequestAction() {
$this->view->setVariablesToRender(array('records'));
$this->view->setConfiguration(array(
'records' => array(
'only' => array('name', 'owner')
)
)
);
$this->view->assign('records', $this->leiRepository->jsonRequest());
}
我仍然得到标准json中的所有字段
这是存储库中的功能:
public function jsonRequest() {
$query = $this->createQuery();
$result = $query->setLimit(1000)->execute();
//$result = $query->execute();
return $result;
}
JsonView
配置只能够过滤/减少数据-这不能用于添加额外的计算属性,如初始问题中所要求的。除此之外,关键字是\u only
,而不是only
您不需要使用
json\u encode()
,仍然可以使用JsonView
。但是,必须在控制器中单独计算数据有效负载-因此,要包含例如recordsTotal
和DT_RowId
属性。在控制器中使用您自己的JsonView:
/**
* JsonController
*/
class JsonController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
/**
* view
*
* @var \TYPO3\CMS\Extbase\Mvc\View\JsonView
*/
protected $view;
/**
* defaultViewObjectName
*
* @var string
*/
protected $defaultViewObjectName = \Vendor\Extension\View\JsonView::class;
/**
* action list
*
* @return void
*/
public function listAction() {
$response = [
'draw' => $draw,
'start' => $start,
'length' => $length,
'recordsTotal' => $allProducts->count(),
'recordsFiltered' => $allProducts->count(),
'order' => $order,
'search' => $search,
'columns' => $columns,
'data' => $filteredProducts
];
$this->view->setVariablesToRender(['response']);
$this->view->assign('response', $response);
}
}
请注意,在控制器中按照DataTables的预期(上面最后一行)装载整个$repsonse。以下是JsonView:
/**
* JsonView
*/
class JsonView extends \TYPO3\CMS\Extbase\Mvc\View\JsonView {
/**
* @var array
*/
protected $configuration = [
'response' => [
//'_only' => ['draw', 'start', 'length', 'order', 'columns', 'recordsTotal', 'recordsFiltered', 'data'],
'data' => [
'_descend' => [
'product' => [
'_only' => ['uid'],
],
'orderType' => [
'_only' => ['uid'],
],
],
'_descendAll' => [
//'_exclude' => ['title'],
'_descend' => [
'maturity' => [],
'currency' => [
'_only' => ['title'],
],
'updated' => [],
'customer' => [
'_only' => ['title', 'title_short', 'titleShort'],
],
],
],
],
],
];
/**
* Always transforming ObjectStorages to Arrays for the JSON view
*
* @param mixed $value
* @param array $configuration
* @return array
*/
protected function transformValue($value, array $configuration) {
if ($value instanceof \TYPO3\CMS\Extbase\Persistence\ObjectStorage) {
$value = $value->toArray();
}
return parent::transformValue($value, $configuration);
}
}
您可以发布来自
$this->lei存储库->jsonRequest()的代码吗?
?来自jsonRequest()存储库的函数?我在上面提供了…您如何获得JSON?没有看到任何json\u encode()
或类似内容。它来自JsonView。我更新了上面的内容。不过,与json_encode类似或乐于使用json_encode