json.loads反斜杠报价中断

时间:2015-03-02 14:46:38

标签: python json

我试图json.load以下字符串

json.loads('''{"summary": "win32k.sys in the kernel-mode drivers in Microsoft Windows Server 2003 SP2, Windows Vista SP2, Windows Server 2008 SP2 and R2 SP1, Windows 7 SP1, Windows 8, Windows 8.1, Windows Server 2012 Gold and R2, and Windows RT Gold and 8.1 allows local users to gain privileges or cause a denial of service (NULL pointer dereference) via a crafted application, aka \"Win32k Elevation of Privilege Vulnerability.\"", "update_date": "2015-02-18T22: 00: 10", "version": 1}''')

但它每次都在\"挣扎。是否有一种简单的方法可以正确地转义这个值,以便json.loads理解它?

1 个答案:

答案 0 :(得分:3)

使用原始字符串r

json.loads(r'''{"summary": "win32k.sys in the kernel-mode drivers in Microsoft Windows Server 2003 SP2, Windows Vista SP2, Windows Server 2008 SP2 and R2 SP1, Windows 7 SP1, Windows 8, Windows 8.1, Windows Server 2012 Gold and R2, and Windows RT Gold and 8.1 allows local users to gain privileges or cause a denial of service (NULL pointer dereference) via a crafted application, aka \"Win32k Elevation of Privilege Vulnerability.\"", "update_date": "2015-02-18T22: 00: 10", "version": 1}''')

js = json.loads(r'''{"summary": "win32k.sys in the kernel-mode drivers in Microsoft Windows Server 2003 SP2, Windows Vista SP2, Windows Server 2008 SP2 and R2 SP1, Windows 7 SP1, Windows 8, Windows 8.1, Windows Server 2012 Gold and R2, and Windows RT Gold and 8.1 allows local users to gain privileges or cause a denial of service (NULL pointer dereference) via a crafted application, aka \"Win32k Elevation of Privilege Vulnerability.\"", "update_date": "2015-02-18T22: 00: 10", "version": 1}''')
print(js)