[英]Python Error:“TypeError: string indices must be integers”
I'm here trying to parse JSON string that comes from a sql table, but while parsing it is giving the error TypeError: string indices must be integers
at line result = json_normalize(json_st,'results')
我在这里尝试解析来自sql表的JSON字符串,但在解析时会出现错误
TypeError: string indices must be integers
行result = json_normalize(json_st,'results')
处的TypeError: string indices must be integers
here is the code snippet 这是代码片段
from pandas.io.json import json_normalize
import pyodbc
cnxn = pyodbc.connect('connection string')
cursor = cnxn.cursor()
cursor.execute("select TOP 1 GEOCODE_ID, JSON from GEOCODE_TBL where GEOCODE_ID = 20")
ID=[]
JSON=[]
for row in cursor.fetchall():
ID.append(row[0])
JSON.append(row[1])
json_st1 = json.dumps(JSON)
json_st=json.loads(json_st1)
result = json_normalize(json_st,'results')
Any suggestion on this will be helpful. 关于此的任何建议将有所帮助。
Thanks 谢谢
Domnick. 多姆尼克。
Looking at the function signature, it takes one data variable and some optional params. 查看函数签名,它需要一个数据变量和一些可选参数。
pandas.io.json.json_normalize(data, record_path=None, meta=None,
meta_prefix=None, record_prefix=None, errors='raise', sep='.')
Where data : dict or list of dicts
. data : dict or list of dicts
。 So you can't pass it as a string . 因此,您不能将其作为字符串传递。 Also, why are you passing the string
'results'
too? 另外,为什么还要传递字符串
'results'
?
Your data variable needs to be a dict, or list of dicts (which is, json in python) 您的数据变量必须是字典或字典列表(即python中的json)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.