从 Django Rest Framework 中的 Json 字符串中删除反斜杠

2024-05-08

dct_data = json_tour_data.__dict__
tour_data = json.dumps(dct_data)

如何删除这些反斜杠json?这是我的输出:

"{\"strFileOpenDateAjxKey\": \"2018-01-16 12:40:22.526417\", 
\"strFilePassengerAjxKey\": \"Zahra Walji\", \"strFileOpenMobileAjxKey\": 
\"46464664\", \"strFileOpenDepartmentAjxKey\": \"Finance department\", 
\"strFileOpenAccountCodeAjxKey\": \"CARTZS\", 
\"strFileOpenProfileCodeAjxKey\": \"CARTZS\", 
\"strFileopenOriginalCountryIdAjxKey\": 61, \"blnBoundAjxKey\": 1, 
\"strTransactionCurrencyJsKey\": \"Shillings\", 
\"intCurrencyPrecisionJsKey\": 3, \"strPackageTypeJsKey\": \"PKG\", 
\"strUserNameAjxKey\": \"admin\", \"strPasswordAjxKey\": \"1234\"}"

答案是你必须玩json.dumps() and json.loads().

以下是我的有效代码:-

import json
json_data = {'key1': 'first'}
json_data = json.dumps(json_data)
return {
    'statusCode': 200,
    'schools':  json.loads(json_data)
}

上述代码的输出如下:

Response:
{
  "schools": {
    "key1": "first"
  },
  "statusCode": 200
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 Django Rest Framework 中的 Json 字符串中删除反斜杠 的相关文章

随机推荐