【问题标题】:json turns to a double quoted string - python djangojson 变成双引号字符串 - python django
【发布时间】:2019-04-15 14:45:46
【问题描述】:

我有一个很奇怪的问题:

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

谢谢。

【问题讨论】:

  • 如果你想在这里找到帮助,你应该展示你的示例代码。
  • 您将字典保存为 Json 字符串。如果要将json字符串转换为json对象,可以使用json.loads
  • @BearBrown:我现在添加了这个,但它是普通的香草形式..
  • @wendelbsilva:这绝对不正确。你可以自己试试。
  • 请显示您的模型和表单定义。请注意,您将字符串称为与 JSON 不同的事物会混淆事物; JSON一种字符串格式。但是,该字符串实际上不是有效的 JSON,所以发生了一些奇怪的事情。

标签: python json django postgresql


【解决方案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 文本表示来交换数据。

【讨论】:

  • 问题是代码库相当大,我正在提取其中的一部分以显示相关信息。因此,例如,当我有数据字典时,我执行 type(data) 并输出 <class 'dict'> 但是,当我执行 type(json.dumps(data)) 时,我得到 <class 'str'> 似乎很奇怪。我所做的只是将数据传递给一个表单,该表单需要一个 JSONB 字段,但是当我保存表单时,我会看到 JSON 的字符串表示形式。
  • 我已经用你的回答解决了这个问题:) 事实证明,代码库有一个函数可以将所有内容转换为字符串 - 非常烦人。无论如何,再次感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2019-08-09
  • 1970-01-01
  • 2020-11-29
  • 2021-11-16
  • 1970-01-01
  • 1970-01-01
  • 2019-10-04
相关资源
最近更新 更多