MySQL转换Json更改列名称

时间:2018-06-12 15:17:00

标签: php mysql json

在一些MySQL查询之后,我将我的JSON编码为在我的应用程序中使用,如;

$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $json = json_encode($rows);
    $response->withHeader('Content-Type', 'application/json');
    $response->getBody()->write($json);
    return $rows;

以下是我的JSON示例;

{list_id : "2", list_name : "name here", list_description : "desc here"}

当然导出与列名相同。像list_id在mysql列名中也是等于

我想改变以使其更安全,有没有快速的方法来做到这一点而不是手动写回声?

1 个答案:

答案 0 :(得分:3)

您可以在SELECT中使用列别名,在结果集中为列提供不同的名称......

pd.DataFrame(data)
Out[173]: 
       ...
    0  ...
    1  ...
    2  ...
    3  ...
    4  ...
    5  ...
    6  ...
    7  ...
    8  ...
    9  ...
    10 ...
    11 ...
    12 ...
   13 ...

    [14 rows x 2 columns]