【发布时间】:2013-08-16 11:39:32
【问题描述】:
我正在尝试将此字符串转换为 python dict:
q = '{"request_body": "{\\x22username\\x22:\\x222\\x22,\\x22password\\x22:\\x226\\x22,\\x22id\\x22:\\x22e2cad174-736e-3041-cf7e\\x22, \\x22FName\\x22:\\x22HS\\x22}", "request_method": "POST"}'
当我做 json.loads(a) 它给我一个错误 simplejson.decoder.JSONDecodeError: Invalid \escape: line 1 column 19 (char 19) 。所以我决定将\x22 转换为"。我听从了this question 的建议。
>>> q.decode('string_escape')
'{"request_body": "{"username":"2","password":"6","id":"e2cad174-736e-3041-cf7e", "FName":"HS"}", "request_method": "POST"}'
>>>
但之后"{" 部分无效。
所以我的问题是将字符串q 转换为python dict 的可能方法是什么?
【问题讨论】:
标签: python json string dictionary unicode-escapes