我有一个很奇怪的问题:
因此,我在python中有一个简单的字典,看起来像这样:
data={'Acoustics': {'Product Type': 'Acoustic Pod', 'Width [cm]': '1000', 'Noise Reduction Coefficient': '29 dB', 'Standards, Certification, and Documentation': 'PN EN-ISO 717-1:1999 ; EN 13501 B, s1, d0', 'Material': 'MDF ; Glass ; Acoustic composite', 'Color': 'NCS ; RAL', 'Installation Method': 'Own assembly ; Installation by the manufacturer', 'Facing Material': 'MDF ; Certified Paint', 'Type': 'Adjustable Ventilation ; Motion Sensor ; LED 6000K 2W ; 230V ; RJ45 or USB Charger ; Integrated seat and shelf'}}
,然后我尝试通过django将其保存到我的pgSQL数据库(带有jsonb列)中,以某种方式结束(注意开头和结尾的双引号):
"{'Acoustics': {'Product Type': 'Acoustic Pod', 'Width [cm]': '1000', 'Noise Reduction Coefficient': '29 dB', 'Standards, Certification, and Documentation': 'PN EN-ISO 717-1:1999 ; EN 13501 B, s1, d0', 'Material': 'MDF ; Glass ; Acoustic composite', 'Color': 'NCS ; RAL', 'Installation Method': 'Own assembly ; Installation by the manufacturer', 'Facing Material': 'MDF ; Certified Paint', 'Type': 'Adjustable Ventilation ; Motion Sensor ; LED 6000K 2W ; 230V ; RJ45 or USB Charger ; Integrated seat and shelf'}}"
要添加到数据库中,我使用django形式,如下所示:
form_data={"cvar": data}
form = myform(form_data)
if form.is_valid():
form.save()
所以,现在,我有两个问题:
[1]如何避免上述情况?为什么会得到quoted
?我只是传递一个表单数据以保存,它最终以某种字符串而不是json的形式结束。
[2]如果我有这样的带引号的json(不幸的是我现在这样做了),我该如何取消引号并将其作为json访问(目前它是该死的字符串!)。
谢谢。
答案 0 :(得分:1)
要弄清没有MCVE showing the relevant code并不容易。
好像您已将数据库中的字典写成字典,然后将其转换成JSON,而不是直接将字典转换成json。
赞:
>>> import json
>>> a={'Acoustics': {'Product Type': 'Acoustic Pod', 'Width [cm]': '1000', 'Noise Reduction Coefficient': '29 dB', 'Standards, Certification, and Documentation': 'PN EN-ISO 717-1:1999 ; EN 13501 B, s1, d0', 'Material': 'MDF ; Glass ; Acoustic composite', 'Color': 'NCS ; RAL', 'Installation Method': 'Own assembly ; Installation by the manufacturer', 'Facing Material': 'MDF ; Certified Paint', 'Type': 'Adjustable Ventilation ; Motion Sensor ; LED 6000K 2W ; 230V ; RJ45 or USB Charger ; Integrated seat and shelf'}}
然后:
>>> print(json.dumps(str(a)))
"{'Acoustics': {'Product Type': 'Acoustic Pod', 'Width [cm]': '1000', 'Noise Reduction Coefficient': '29 dB', 'Standards, Certification, and Documentation': 'PN EN-ISO 717-1:1999 ; EN 13501 B, s1, d0', 'Material': 'MDF ; Glass ; Acoustic composite', 'Color': 'NCS ; RAL', 'Installation Method': 'Own assembly ; Installation by the manufacturer', 'Facing Material': 'MDF ; Certified Paint', 'Type': 'Adjustable Ventilation ; Motion Sensor ; LED 6000K 2W ; 230V ; RJ45 or USB Charger ; Integrated seat and shelf'}}"
代替:
>>> print(json.dumps(a))
{"Acoustics": {"Product Type": "Acoustic Pod", "Width [cm]": "1000", "Noise Reduction Coefficient": "29 dB", "Standards, Certification, and Documentation": "PN EN-ISO 717-1:1999 ; EN 13501 B, s1, d0", "Material": "MDF ; Glass ; Acoustic composite", "Color": "NCS ; RAL", "Installation Method": "Own assembly ; Installation by the manufacturer", "Facing Material": "MDF ; Certified Paint", "Type": "Adjustable Ventilation ; Motion Sensor ; LED 6000K 2W ; 230V ; RJ45 or USB Charger ; Integrated seat and shelf"}}
如果您已经将python字典表示形式作为来自某些外部数据源的字符串,则可以use ast.literal_eval() to turn it to a proper dict first:
>>> the_dict=ast.literal_eval(the_data)
>>> the_json=json.dumps(the_dict)
或者最好将数据源(例如Web表单)更改为使用JSON格式,而不是Python dict文本表示形式来交换数据。