[英]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.