正在解析我的json,我收到此“ JSONDecodeError:无效的\ escape”

问题描述 投票:0回答:2
my_json = '{"hello":"\\x20\\x20\\x20\\x3Cdiv\\x3E\\x20\\x0A\\x20\\x20\\x20\\x20\\HELLO"}'

json.loads(my_json)

我明白了。

JSONDecodeError: Invalid \escape: line 1 column 11 (char 10)

我需要使用什么转换才能加载json?

python json python-3.x scrapy html-escape-characters
2个回答
1
投票

首先将python字符串转换为json,它将为您服务。my_json = json.dumps({“ hello”:“ \ x20 \ x20 \ x20 \ x3Cdiv \ x3E \ x20 \ x0A \ x20 \ x20 \ x20 \ x20 \ x20 \ HELLO”})json.loads(my_json)


0
投票

@ pguardiario的建议肯定更干净,但是如果您只想得到结果dict,请尝试eval

In [77]: my_json                                                                
Out[77]: '{"hello":"\\x20\\x20\\x20\\x3Cdiv\\x3E\\x20\\x0A\\x20\\x20\\x20\\x20\\HELLO"}'

In [78]: evaluated = eval(my_json)                                                          
Out[78]: {'hello': '   <div> \n    \\HELLO'}

In [79]: evaluated.items()                                                      
Out[79]: dict_items([('hello', '   <div> \n    \\HELLO')])
© www.soinside.com 2019 - 2024. All rights reserved.