2015-10-15 69 views
0

我在python中运行Postgres查询,它返回一个JSON对象数组。python json转储将对象放入row_to_json对象返回

db = SQLDB('postgres://postgres:*****@localhost:5433/postgres', migrate=False) 
    q = ("SELECT row_to_json(t) FROM (SELECT * FROM fn_drivetime_eu_grid_" + request.get_vars.country + "_coord(" + request.get_vars.x + ", " + request.get_vars.y + ", " + request.get_vars.sec + ")) t;") 
    mySQL = db.executesql(q) 
    return json.dumps(mySQL) 

问题是,对象是在对象内部。耶!

不是一个大问题,但我想知道是否有一个更优雅的解决方案。

enter image description here

+0

显示的Python代码和查询输出。 –

+0

我扩展了代码示例。 –

+0

任何机会我们可以看到原始的JSON,而不是目前显示的任何工具? –

回答

0

也就是说,如果你放弃整个结果集发生什么。随着t表:

create table t (a int, b text); 
insert into t (a, b) values (1,'x'), (2,'y'); 

使用Psycopg2:

query = "select row_to_json(t) from t" 
cursor.execute(query) 
rs = cursor.fetchall() 

# dump the whole result set 
print json.dumps(rs) 
print 

# dump each column: 
for r in rs: 
    print json.dumps(r[0]) 
con.close() 

输出:

[[{"a": 1, "b": "x"}], [{"a": 2, "b": "y"}]] 

{"a": 1, "b": "x"} 
{"a": 2, "b": "y"}