json.loads()不能转换成字典



我在表中解析json字符串中的特定键时遇到了麻烦。下面是我读取csv文件并提取"employee_id"的代码从每一行的json列中:

with open('data.csv') as csvFile:
csv_reader = csv.reader(csvFile, delimiter=',')
next(csv_reader, None)  # skips the header row
for row in csv_reader:
event_data = row[4]
data = json.loads(event_data)
print(data['employee_id'])

下面是一个event_data输出示例:

"{"py/object": "employee_information.event_types.EmployeeCreated", "employee_id": "98765", "employee_first_name": "Jonathan", "employee_last_name": "Smith", "application_id": "1234", "address": "1234 street"}"

但是我得到一个错误说:

Traceback (most recent call last):
File "/Users/user/Documents/python_test/main.py", line 14, in <module>
print(data['employee_id'])
TypeError: string indices must be integers

我检查了数据的类型,它返回一个str。负载应该将json字符串转换为python字典?

event_data由于某种原因是双编码的,所以您需要对其进行两次解码。

data = json.loads(json.loads(event_data))

最新更新