繁体   English   中英

json 变成双引号字符串 - python django

[英]json turns to a double quoted string - python django

我有一个比较奇怪的问题:

所以,我在 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 访问(目前它是一个该死的字符串!)。

谢谢你。

如果没有MCVE 显示相关代码,就不容易弄清楚。

看起来你给数据库写了一个转换为字符串然后转换为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 字典表示为来自某个外部数据源的字符串,那么您可以先使用 ast.literal_eval() 将其转换为正确的 dict

>>> the_dict=ast.literal_eval(the_data)
>>> the_json=json.dumps(the_dict)

或者,最好将数据源(例如 Web 表单)更改为使用 JSON 格式而不是 Python dict 文本表示来交换数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM