2017-02-27 71 views
1

我如何可以操纵/更改返回JSON:TYPO3 Extbase - 操纵/更改返回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 
}] 

到如:

{ 
"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; 

} 
+0

你能从'$ this-> leiRepository-> jsonRequest()'发布代码吗? –

+0

从jsonRequest()的存储库中的函数?我上面提供了... –

+0

你如何获得JSON?没有看到任何'json_encode()'或类似的。 –

回答

0

JsonView的配置仅仅是能够过滤的/减少数据 - 这不能用于添加像像在最初的问题请求的附加计算性能。除此之外,关键字是_only而不是only

您不需要使用json_encode(),仍然可以使用JsonView。但是,数据有效载荷必须在您的控制器中单独计算 - 因此,要包含例如recordsTotalDT_RowId属性。