如何将\ x22这样的字符转换为字符串? [英] How to convert characters like \x22 into a string?

查看:540
本文介绍了如何将\ x22这样的字符转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的字符串:

I have a string that looks like this:

"{\\x22username\\x22:\\x229\\x22,\\x22password\\x22:\\x226\\x22,\\x22id\\x22:\\x222c8bfa56-f5d9\\x22, \\x22FName\\x22:\\x22AnkQcAJyrqpg\\x22}"

据我了解,

\x22".那么如何将其转换为带有键和值引号的可读JSON?

as far as I understand \x22 is ". So how could I convert this into a readable JSON with quotes around keys and values?

推荐答案

string_escape解码:

>>> import json
>>> value = "{\\x22username\\x22:\\x229\\x22,\\x22password\\x22:\\x226\\x22,\\x22id\\x22:\\x222c8bfa56-f5d9\\x22, \\x22FName\\x22:\\x22AnkQcAJyrqpg\\x22}"
>>> value.decode('string_escape')
'{"username":"9","password":"6","id":"2c8bfa56-f5d9", "FName":"AnkQcAJyrqpg"}'
>>> json.loads(value.decode('string_escape'))
{u'username': u'9', u'password': u'6', u'id': u'2c8bfa56-f5d9', u'FName': u'AnkQcAJyrqpg'}

这篇关于如何将\ x22这样的字符转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆