将数据格式化为JSON

时间:2015-05-19 15:42:01

标签: json postgresql python-2.7

我使用python

从PostgreSQL获取数据
    select submitter, issue, 
       (select max(q1) 
           from survey 
          where submitter = parent.submitter 
            and issue = parent.issue) as q1,
       (select max(q2) 
           from survey 
          where submitter = parent.submitter 
            and issue = parent.issue) as q2,
       (select max(q3) 
           from survey 
          where submitter = parent.submitter 
            and issue = parent.issue) as q3,
       (select max(q4) 
           from survey 
          where submitter = parent.submitter 
            and issue = parent.issue) as q4,
       (select max(q5) 
           from survey 
          where submitter = parent.submitter 
            and issue = parent.issue) as q5
from survery as parent
group by submitter, issue; 

然后我尝试将此数据格式化为JSON格式:使用以下命令:

alter table survery add id bigint auto_increment;

但我在一个单独的行中得到了ID,姓名,地址......的每个字样:

例如:name = abc 执行脚本时我得到:

B'/ P>

C

2 个答案:

答案 0 :(得分:2)

您的结果(rows)是一个字符串(这是json.dumps()返回的内容)。这就是为什么当你通过行迭代时,你正在迭代并打印每个字符。

只需改为print rows

答案 1 :(得分:0)

json.dumps()返回一个字符串,因此迭代返回每个字符。

要获取信息流,请使用json.dump()