我正在尝试将以下(json)字符串转换为python数据类型:
data = "{'id': 26, 'photo': '/media/f082b5af-ad0.png', 'first_name': 'Islam', 'last_name': 'Mansour', 'email': '[email protected]', 'city': 'Giza', 'cv': '/media/fbb61609-442.pdf', 'reference': 'Facebook', 'campaign': OrderedDict([('id', 2), ('name', 'javascript')]), 'status': 'Invitation Sent', 'user': None, 'at': '2020-01-20', 'time': '23:02:58.359179', 'technologies': [OrderedDict([('id', 46), ('name', 'Django'), ('category', OrderedDict([('id', 24), ('name', 'Framework'), ('_type', 'skill')]))])]}"
我正在尝试通过使用将其转换为JSON
json.loads(data.replace(“ \'”,“ \”“))
但我遇到以下错误
json.decoder.JSONDecoderError: Expecting value: line 1 column 219 (char 218)
如前所述,OrderedDict不是正确的JSON。但这是正确的python。
要解决这个问题:
from collections import OrderedDict # direct import because this is as this in your string
import json
jsonCorrect = json.dumps(eval(data))
json.loads(jsonCorrect) # it works
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句