作为字符串的 CakePHP 响应编号

CakePHP response numbers as strings

CakePHP 在json 响应中将数字序列化为字符串,像这样{"foo": "123"},应该是{"foo": 123 } 有什么配置吗?

    $stm = "select * from clients";
    $response = $conn->execute($stm)->fetchAll('assoc');

    $this->set([
        'success' => $success,
        'code' => $code,
        'response' => $response,
        'errors' => $errors,
        '_serialize' => ['success', 'code', 'response', 'errors'],
    ]);

你可以像这样传递 json_encode 选项

$this->set('_jsonOptions', JSON_NUMERIC_CHECK);
$this->set('_serialize', ['success', 'code', 'response', 'errors']);

https://book.cakephp.org/3/en/views/json-and-xml-views.html#creating-json-views