Python JSON解码字符串
我正在尝试使用Python JSON解码字符串,python,json,python-2.7,Python,Json,Python 2.7,我正在尝试使用 json.loads(request.POST.get('d')) 其中d是包含JSON字符串的POST参数 我在stacktrace中发现以下错误: ValueError: Unterminated string starting at: line 1 column 22 (char 22) 这是JSON字符串: {"data":{"40":{"html":"<span style=\"color:#ffffff;\">test</span>","b
json.loads(request.POST.get('d'))
其中d是包含JSON字符串的POST参数
我在stacktrace中发现以下错误:
ValueError: Unterminated string starting at: line 1 column 22 (char 22)
这是JSON字符串:
{"data":{"40":{"html":"<span style=\"color:#ffffff;\">test</span>","background":"transparent"},"41":{"html":"","background":"transparent"},"42":{"html":"","background":"transparent"}},"action":"save"}
这里有什么问题?我想源字符串中有反斜杠 当我解析
"""{"data":{"40":{"html":"<span style=\"color:#ffffff;\">test</span>","background":"transparent"},"41":{"html":"","background":"transparent"},"42":{"html":"","background":"transparent"}},"action":"save"}"""
“{”数据“{”40“{”html“:“测试”,“背景“:“透明”}”,41“{”html“:“背景“:“透明”}”,42“{”html“:“背景“:“透明”}”,操作“:“保存”}”
对于json.loads(),它会失败,并出现类似的错误
但是,当我禁用转义序列(r''字符串文字)时,它可以工作:
r"""{"data":{"40":{"html":"<span style=\"color:#ffffff;\">test</span>","background":"transparent"},"41":{"html":"","background":"transparent"},"42":{"html":"","background":"transparent"}},"action":"save"}"""
r”“{”data:{“40:{”html:“test”,“background:“transparent”},“41:{”html:“background:“transparent”},“42:{”html:“background:“transparent”},“action:“save”}”
显然,当您构造字符串时,字符串中的'\'
将被转义,并导致''
,可能是在JS(?)中。尚未看到构建它的代码,但请尝试添加一个额外的反斜杠:“\\”
更新:您可以将字符串中的
r'\'
替换为r'\\'
。但最好了解字符串的开头。当您将字符串正文插入邮件时,它是从哪里获得的?我想源字符串中有带反斜杠的内容
当我解析
"""{"data":{"40":{"html":"<span style=\"color:#ffffff;\">test</span>","background":"transparent"},"41":{"html":"","background":"transparent"},"42":{"html":"","background":"transparent"}},"action":"save"}"""
“{”数据“{”40“{”html“:“测试”,“背景“:“透明”}”,41“{”html“:“背景“:“透明”}”,42“{”html“:“背景“:“透明”}”,操作“:“保存”}”
对于json.loads(),它会失败,并出现类似的错误
但是,当我禁用转义序列(r''字符串文字)时,它可以工作:
r"""{"data":{"40":{"html":"<span style=\"color:#ffffff;\">test</span>","background":"transparent"},"41":{"html":"","background":"transparent"},"42":{"html":"","background":"transparent"}},"action":"save"}"""
r”“{”data:{“40:{”html:“test”,“background:“transparent”},“41:{”html:“background:“transparent”},“42:{”html:“background:“transparent”},“action:“save”}”
显然,字符串中的'\'
正在被转义,并在构造字符串时导致'''
,可能是在JS(?)中。尚未看到构建该字符串的代码,但请尝试添加一个额外的反斜杠:'\'
更新:您可以将字符串中的
r'\'
替换为r'\'
。但是最好先了解字符串的外观。当您将字符串正文插入到邮件中时,您从何处获得它?您如何知道这是您获得的字符串?这对我很有用:
>>> ss = r'{"data":{"40":{"html":"<span style=\"color:#ffffff;\">test</span>","background":"transparent"},"41":{"html":"","background":"transparent"},"42":{"html":"","background":"transparent"}},"action":"save"}'
>>> json.loads(ss)
{u'action': u'save', u'data': {u'42': {u'html': u'', u'background': u'transparent'}, u'40': {u'html': u'<span style="color:#ffffff;">test</span>', u'background': u'transparent'}, u'41': {u'html': u'', u'background': u'transparent'}}}
>>ss=r'{“data”:{“40”:{“html”:“test”,“background”:“transparent”},“41”:{“html”:“background”:“transparent”},“42”:{“html”:“background”:“transparent”},“action”:“save”}
>>>json.loads(ss)
{u'action':u'save',u'data':{u'html':u',u'background':u'transparent'},u'40':{u'html':u'test',u'background':u'transparent'},u'41':{u'html':u',u'background':u'transparent'}
请注意,我为
ss
使用了一个原始字符串,因为否则\“
将被字符串中的”
替换,从而导致'“test”
由于明显的原因不起作用。您如何知道这是您得到的字符串?这对我很有用:
>>> ss = r'{"data":{"40":{"html":"<span style=\"color:#ffffff;\">test</span>","background":"transparent"},"41":{"html":"","background":"transparent"},"42":{"html":"","background":"transparent"}},"action":"save"}'
>>> json.loads(ss)
{u'action': u'save', u'data': {u'42': {u'html': u'', u'background': u'transparent'}, u'40': {u'html': u'<span style="color:#ffffff;">test</span>', u'background': u'transparent'}, u'41': {u'html': u'', u'background': u'transparent'}}}
>>ss=r'{“data”:{“40”:{“html”:“test”,“background”:“transparent”},“41”:{“html”:“background”:“transparent”},“42”:{“html”:“background”:“transparent”},“action”:“save”}
>>>json.loads(ss)
{u'action':u'save',u'data':{u'html':u',u'background':u'transparent'},u'40':{u'html':u'test',u'background':u'transparent'},u'41':{u'html':u',u'background':u'transparent'}
请注意,我为ss
使用了一个原始字符串,因为否则\“
将被字符串中的”
替换,从而导致'“test”
由于明显的原因不起作用。这对我们有效:
json.loads(request.POST.get('d').encode('string-escape'))
这对我们起了作用:
json.loads(request.POST.get('d').encode('string-escape'))
啊,好吧,这听起来像是个问题。如何将变量转换为原始字符串?要使字符串成为“原始”字符串,只需在其前面加上
r
:r“这是一个原始字符串”
<代码>“这不是原始字符串”。如果你问的是“变量”,那么这对“变量”不起作用:foo=“bar”;rbar#不会将bar设为原始字符串
在将其插入邮件之前是否已打印request.POST.get('d')?有必要了解它到底包含什么。用r'\'替换r'\'可能就足够了。html来自用户输入文本区域,所以用r'\'替换r'\'的问题是,如果用户显式键入'\'它将被'\\'替换…啊,好的,听起来像是问题。如何将变量转换为原始字符串?要使字符串成为“原始”字符串,只需在其前面加上r
:r“这是一个原始字符串”
<代码>“这不是原始字符串”。如果你问的是“变量”,那么这对“变量”不起作用:foo=“bar”;rbar#不会将bar设为原始字符串
在将其插入邮件之前是否已打印request.POST.get('d')?有必要了解它到底包含什么。用r'\'替换r'\'可能就足够了。html来自用户输入文本区域,因此用r'\'替换r'\'的问题是,如果用户显式键入'\',它将被替换为'\'。字符串来自html wysiwyg文本区域检查用户输入的反斜杠符号会发生什么情况。他们现在变成了双重反斜杠,没有替换吗?不,他们没有,但我不认为这是问题所在。似乎request.POST.get('d')被截断,字符串中有分号。然后尝试用一对反斜杠替换一个反斜杠,并查看结果。也许你会满意的。我刚刚检查过,反斜杠正在被转义为双反斜杠。正如我所说,问题似乎是分号。如果我发送{“data”:“te;st”}request.POST.get('d')中的所有内容都是:{“data”:“该字符串来自html wysiwyg文本区域检查反斜杠符号在出现错误时会发生什么情况