【问题标题】:psycopg2 select result gets nested with additional arraypsycopg2 选择结果与附加数组嵌套
【发布时间】:2017-11-02 17:59:28
【问题描述】:

我正在尝试使用“psycopg2”从 postgres 获取 JSON 行。记录的值类似于 [ [{....},{...},{...}] ]。要获得正确的 JSON 结果,即。 [{....},{...},{...}] 我必须去记录1。不知道为什么会这样。

import psycopg2
import sys
import json

conn_string = "'host='localhost' dbname='postgres' user='postgres' password='password123'" 
con=psycopg2.connect(conn_string)
cur = con.cursor()
cur.execute("select json_agg(art) from (select a.*, (select json_agg(b) from (select * from pfstklist where pfportfolioid = a.pfportfolioid ) as b) as pfstklist, (select json_agg(c) from (select * from pfmflist where pfportfolioid = a.pfportfolioid ) as c) as pfmflist from pfmaindetail as a) art")
records = cur.fetchall()
print(records)     #This gives result [[{....},{...},{...}]]
records1=records[0]    
print(records1)   #This gives expected result [{....},{...},{...}]

【问题讨论】:

  • 是python吗?!!标记语言

标签: python json postgresql psycopg2


【解决方案1】:

cur.fetchall() 为您提供记录列表。你可能想要cur.fetchone()。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    • 2020-05-07
    • 2021-12-06
    相关资源
    最近更新 更多