使用特殊字符“

时间:2018-05-15 10:54:24

标签: python json special-characters

我在python中解析JSON字符串时遇到问题。我有以下字符串:

{"{\"rid\":\"PHMJ3BD77P81B225A0GM\",\"sid\":\"259-2748551-5638001\",\"mid\":\"A1PA6795UKMFR9\",\"sn\":\"www.amazon.de\",\"reqs\":[{\"cap-ciba\":{\"k\":\"fwcim\",\"t\":1522150437949,\"md\":\" +I42uPPjwZf0N \",\"p\":\"https://www.amazon.de/ap/signin?clientContext":"257-0866472-4844452","openid.return_to":"https%3A%2F%2Fwww.amazon.de%2Fkindle-dbs%2Fku%2Fku-central%3Fref_%3Dnav_youraccount_ku%26_encoding%3DUTF8","openid.identity":"http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select","openid.assoc_handle":"amzn_kplp_desktop_de","openid.mode":"checkid_setup","marketPlaceId":"A1PA6795UKMFR9","openid.claimed_id":"http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select","pageId":"amzn_aui_light","openid.ns":"http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0","openid.pape.max_auth_age":"3600","siteState":"clientContext%3D259-2748551-5638001%2CsourceUrl%3Dhttps%253A%252F%252Fwww.amazon.de%252Fkindle-dbs%252Fku%252Fku-central%253Fref_%253Dnav_youraccount_ku%2526_encoding%253DUTF8%2Csignature%3DikQPbHmIyj2F3VCmlAXOMRBtpWIRAj3D\"}}]}"}

字符串来自我在亚马逊上捕获的HTTP请求。因此,我无法控制输入。

我想在python中自动解析这个String(使用json.loads())。但是,解析字符串不起作用(Expecting : delimiter: line 1 column 5 (char 4))。 如果我使用json.laods(r“”“[my_input_string]”“”)(将字符串转换为“原始字符串”),对象将被成功解析。

如何告诉Python将字符串解释为原始字符串?我已经尝试了.escape("string_escape").repalce(“\\”, “\\\\”),但两者都没有用。

提前致谢。

1 个答案:

答案 0 :(得分:0)

您可以使用literal_eval删除所有转义序列。

In [29]: x = {"{\"rid\":\"PHMJ3BD77P81B225A0GM\",\"sid\":\"259-2748551-5638001\",\"mid\":\"A1PA6795UKMFR9\",\"sn\":\"www.amazon.de\",\"reqs\":[{\"cap-ciba\":{\"k\":\"fwcim\",\"t\
    ...: ":1522150437949,\"md\":\" +I42uPPjwZf0N \",\"p\":\"https://www.amazon.de/ap/signin?clientContext":"257-0866472-4844452","openid.return_to":"https%3A%2F%2Fwww.amazon.de%2
    ...: Fkindle-dbs%2Fku%2Fku-central%3Fref_%3Dnav_youraccount_ku%26_encoding%3DUTF8","openid.identity":"http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select","openid.
    ...: assoc_handle":"amzn_kplp_desktop_de","openid.mode":"checkid_setup","marketPlaceId":"A1PA6795UKMFR9","openid.claimed_id":"http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fide
    ...: ntifier_select","pageId":"amzn_aui_light","openid.ns":"http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0","openid.pape.max_auth_age":"3600","siteState":"clientContext%3D259-274
    ...: 8551-5638001%2CsourceUrl%3Dhttps%253A%252F%252Fwww.amazon.de%252Fkindle-dbs%252Fku%252Fku-central%253Fref_%253Dnav_youraccount_ku%2526_encoding%253DUTF8%2Csignature%3Dik
    ...: QPbHmIyj2F3VCmlAXOMRBtpWIRAj3D\"}}]}"}

In [30]: from ast import literal_eval

In [31]: literal_eval(str(x))
Out[31]:
{'marketPlaceId': 'A1PA6795UKMFR9',
 'openid.assoc_handle': 'amzn_kplp_desktop_de',
 'openid.claimed_id': 'http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select',
 'openid.identity': 'http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select',
 'openid.mode': 'checkid_setup',
 'openid.ns': 'http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0',
 'openid.pape.max_auth_age': '3600',
 'openid.return_to': 'https%3A%2F%2Fwww.amazon.de%2Fkindle-dbs%2Fku%2Fku-central%3Fref_%3Dnav_youraccount_ku%26_encoding%3DUTF8',
 'pageId': 'amzn_aui_light',
 'siteState': 'clientContext%3D259-2748551-5638001%2CsourceUrl%3Dhttps%253A%252F%252Fwww.amazon.de%252Fkindle-dbs%252Fku%252Fku-central%253Fref_%253Dnav_youraccount_ku%2526_encoding%253DUTF8%2Csignature%3DikQPbHmIyj2F3VCmlAXOMRBtpWIRAj3D"}}]}',
 '{"rid":"PHMJ3BD77P81B225A0GM","sid":"259-2748551-5638001","mid":"A1PA6795UKMFR9","sn":"www.amazon.de","reqs":[{"cap-ciba":{"k":"fwcim","t":1522150437949,"md":" +I42uPPjwZf0N ","p":"https://www.amazon.de/ap/signin?clientContext': '257-0866472-4844452'}

In [32]: