我需要通过应用程序接口请求PG数据库,我使用的是Flask requests
包:
payload = {'key':'**', 'schema':'**', 'table':'testh','where_clause':'0', 'liste_fields':'*'}
r = requests.get('https://myapi/', params=payload, verify=False)
我需要将我的testh
表存储的所有内容放在一个JSON中,但是函数r.json()
得到了这个无效的JSON:
{'id': {'0': '1', '1': '2', '2': '3', '3': '4'}, 'brand': {'0': 'apple', '1': 'microsoft', '2': 'google', '3': 'amazon'}}
我需要一个JSON as:{0:{id:'2',品牌:‘apple},1:{id:'2',品牌:’microsoft},....}
发布于 2020-04-07 01:59:16
您可以使用pandas来执行此操作:
import pandas as pd
d = {'id': {'0': '1', '1': '2', '2': '3', '3': '4'}, 'brand': {'0': 'apple', '1': 'microsoft', '2': 'google', '3': 'amazon'}}
d = pd.DataFrame(d).to_dict(orient='index')
输出:
{'0': {'id': '1', 'brand': 'apple'},
'1': {'id': '2', 'brand': 'microsoft'},
'2': {'id': '3', 'brand': 'google'},
'3': {'id': '4', 'brand': 'amazon'}}
这只是一个猜测,但可能API也使用了pandas,如果不带参数地调用to_dict(),就会得到所描述的输出。
https://stackoverflow.com/questions/61060706
复制相似问题