我正在用JSON格式编写一些数据文件,并希望将一些非常长的字符串值分成多行.使用python的JSON模块我得到了很多错误,无论是使用\还是\n作为转义.
是否可以在JSON中使用多行字符串?这主要是为了视觉上的舒适,所以我想我可以在我的编辑器中翻开自动换行,但我只是有点好奇......
根据这个答案,应始终转义JSON字符串中的换行符.当我加载JSON时,这似乎不是必需的json.load().
我已将以下字符串保存到文件中:
{'text': 'Hello,\n How are you?'}
Run Code Online (Sandbox Code Playgroud)
加载JSON json.load()并不会引发异常,即使\n没有转义:
>>> with open('test.json', 'r') as f:
... json.load(f)
...
{'text': 'Hello,\n How are you?'}
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用json.loads(),我会得到一个例外:
>>> s
'{"text": "Hello,\n How are you?"}'
>>> json.loads(s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\Python34\lib\json\__init__.py", line 318, in loads
return _default_decoder.decode(s)
File "c:\Python34\lib\json\decoder.py", line 343, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "c:\Python34\lib\json\decoder.py", line 359, in raw_decode
obj, …Run Code Online (Sandbox Code Playgroud) 我得到一个JSON字符串,其中有一个"\r"字符,例如"{"data":"foo \r\n bar"}"当我尝试解析它时抛出ValueError.
>>> j="""{"data":"foo \r\n bar"}"""
>>> import json
>>> f=json.loads(j)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
f=json.loads(j)
File "C:\Python27\lib\json\__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Invalid control character at: line 1 column 13 (char 13)
>>> j[13]
'\r'
Run Code Online (Sandbox Code Playgroud)
"\r" 是Python字符串中完全合法的字符.
我该如何解析这个JSON字符串呢
>>> dct …Run Code Online (Sandbox Code Playgroud) 这是Json数据:
{
"maxPage" : 145,
"previous_cursor" : null,
"next_cursor": 1420,
"data": {
"2427459624": {
"nick": "\u5c0f\u767d\u6843\u82b1\u773cGy",
"fans": 565,
"vip": 0,
"avantar": "http: \/\/tp1.sinaimg.cn\/2427459624\/30\/5614847484\/0",
"ta": "\u5979",
"relation": 0,
"canMsg": 0,
"vipReason": "",
"description": "\u5f88\u591a\u65f6\u5019\u4e36\u535f\u53bb\u8bf4\u4e36\u535f\u53bb\u505a\u4e36\u535f\u53bb\u60f3\u4e36\u535f\u4ee3\u8868\u535f\u5728\u4e4e\u3002",
"location": "\u9ed1\u9f99\u6c5f \u7261\u4e39\u6c5f",
"text": "@\u975e\u9c7c-CC \u6211\u56de\u8d60\u4e86\u6e29\u99a8\u793c\u76d2\u7ed9\u4f60\u4eec\u3002\u4e00\u8d77\u6765\u73a9\u5fae\u57ce\u5e02\u5427\uff01\u5f00\u59cb\u6e38\u620fhttp: \/\/t.cn\/ak39KS",
"textTime": "\u4eca\u5929 13: 30",
"distance": ""
},
"2574743404": {
"nick": "\u798f\u5efa\u65f6\u5c1a\u751f\u6d3b",
"fans": 52,
"vip": 0,
"avantar": "http: \/\/tp1.sinaimg.cn\/2574743404\/30\/5618976622\/0",
"ta": "\u5979",
"relation": 0,
"canMsg": 0,
"vipReason": "",
"description": "\u798f\u5efa\u65f6\u5c1a\u751f\u6d3b\u7cbe\u5f69\u8d44\u8baf\u63a8\u8350",
"location": "\u798f\u5efa",
"text": "\u5206\u4eab\u76f8\u518c\uff1aJil Sander 2012\u6625\u590f\u6d41\u884c\u53d1\u5e03 \uff08\u914d\u9970\uff09\u3001\u7cbe\u5f69\u56fe\u7247\u63a8\u8350\uff1aJil Sander 2012\u6625\u590f\u6d41\u884c\u53d1\u5e03 \uff08\u914d\u9970\uff09 [78]\uff0809\u670826\u65e5\u4e0a\u4f20\uff09\u3001\u6d4f\u89c8\u5168\u90e884\u5f20\u8d85\u9ad8\u6e05\u5927\u56fe http: …Run Code Online (Sandbox Code Playgroud)