我试图防止value
POST 请求中的字符串(在本例中为变量)被转义,因为它要存储在 JSON 中。我的代码是
def addProduct(request):
if request.POST:
post = {}
for key in request.POST:
value = request.POST[key].encode('utf-8')
try:
value = json.loads(value).encode('utf-8')
except Exception:
pass
post[key] = value.encode('utf-8')
doc = json.dumps(post)
我可以看到的调试value
是 unicode 类型,我相信这是 Django 处理请求对象的方式。实际的字符串虽然 unicode 直到post[key] = value
. 如果我尝试更改它post[key] = value.encode('utf-8')
以防止它被转义,我会收到错误消息:'ascii' codec can't decode byte 0xe2 in position 38: ordinal not in range(128)
有任何想法吗?