json变成了一个双引号字符串——python django
我有一个很奇怪的问题: 我有一个简单的python字典,看起来是这样的:json变成了一个双引号字符串——python django,python,json,django,postgresql,Python,Json,Django,Postgresql,我有一个很奇怪的问题: 我有一个简单的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', 'Mater
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列)中,最终我得到了(注意开始和结束处的双引号):
为了添加到数据库中,我使用django表单,如下所示:
form_data={"cvar": data}
form = myform(form_data)
if form.is_valid():
form.save()
现在,我有两个问题:
[1] 如何避免上述情况?为什么它会被引用?我只需传递一个表单数据来保存,它以字符串而不是json的形式结束。
[2] 如果我有这样的带引号的json(不幸的是,我现在有),我如何将其作为json(目前它是一个该死的字符串!)进行解压缩和访问
谢谢你。没有它很难弄清楚 看起来您向数据库写入了一个转换为字符串的字典,然后转换为JSON,而不是直接将字典转换为JSON 比如: 然后:
>>> 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字典表示为来自某个外部数据源的字符串,则可以:
或者,最好将数据源(例如web表单)更改为使用JSON格式而不是Python dict文本表示法来交换数据。如果您想在此处找到帮助,则应显示示例代码。您正在将字典保存为JSON字符串。如果您想将json字符串转换为json对象,您可以使用@BearBrown:我现在添加了这个,但它是普通的表单。@wendelbsilva:这绝对不正确。您可以自己尝试。请显示您的模型和表单定义。注意,将字符串称为与JSON不同的东西会使事情变得混乱;JSON是一种字符串格式。然而,这个字符串实际上不是有效的JSON,所以有些奇怪的事情发生了。问题是代码库相当大,我正在提取它的一部分以显示相关信息。例如,当我有数据字典时,我会
键入(数据)
,它会输出
,但当我执行键入(json.dumps(数据))
时,我会得到
,这看起来很奇怪。我所做的就是将数据传递给表单,表单需要一个JSONB字段,但当我保存表单时,我看到了JSON的字符串表示形式。我已经用您的答案解决了它:)事实证明,代码库有一个函数可以将所有内容转换为字符串-非常烦人。无论如何,再次感谢你的帮助。
>>> 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"}}
>>> the_dict=ast.literal_eval(the_data)
>>> the_json=json.dumps(the_dict)