4

可能重复:
Python JSON 序列化 Decimal 对象

我有以下 SQL:

SELECT 
    concat(UNIX_TIMESTAMP(date), '000') as datetime, 
    SUM(royalty_price) as sales 
FROM 
    sales_raw 
GROUP BY 
    datetime

结果看起来像:

datetime        sales
1337151600000   1045.71 (decimal)
1337238000000   478.04
1337324400000   300.96
1337410800000   289.02

由此我得到以下错误:

Exception Type: TypeError at /ajax/graph/
Exception Value: Decimal('1045.71') is not JSON serializable

我如何将小数转换为 mysql 中可接受的格式以便能够序列化这些数据?

4

1 回答 1

4

如果你必须在 MySQL 中做,只CAST需要一个字符串:

SELECT 
    CONCAT(UNIX_TIMESTAMP(date), '000') AS datetime, 
    CAST(SUM(royalty_price) AS CHAR)    AS sales
FROM 
    sales_raw 
GROUP BY 
    datetime
于 2012-05-31T20:02:02.390 回答