4

我正在向带有后端jsonrpc的服务器发送一些请求。有时,我会遇到想要分析的错误。错误在回复中转义了,所以不容易理解。我得到这样的东西:web2pyceleryjsonrpc

{"version": "1.1", "id": "ID4", "error": {"message": "TypeError: 'NoneType' object does not support item assignment", "code": 100, "data": ["  File \"/home/myuser1/tmp/web2py/gluon/tools.py\", line 4068, in serve_jsonrpc\n    s = methods[method](*params)\n", "  File \"/home/myuser1/tmp/web2py/applications/mycompany_portal/controllers/activity.py\", line 66, in get_cdr_page\n    invalidate_cache = pars['invalidate_cache'], use_long_polling = pars['use_long_polling'])\n", "  File \"/home/myuser1/projects/new-mycompany-portal/python_modules/pmq_client.py\", line 85, in get_page\n    res = result.get(timeout=10)\n", "  File \"/home/myuser1/.virtualenvs/python2.7.2-mycompany1/lib/python2.7/site-packages/celery/result.py\", line 119, in get\n    interval=interval)\n", "  File \"/home/myuser1/.virtualenvs/python2.7.2-mycompany1/lib/python2.7/site-packages/celery/backends/amqp.py\", line 138, in wait_for\n    raise self.exception_to_python(meta['result'])\n"], "name": "JSONRPCError"}}

我想要的是获得回复的error.data一部分,将其jsonrpc取消转义并将其显示为堆栈跟踪。我可以手动完成(更改\"->"并处理\n),但我想避免在这里重新发明轮子。

4

2 回答 2

3

这是未解析的原始 JSON 吗?将其解析为 JSON:

import json
print ''.join(json.loads(yourstring)['error']['data'])
于 2012-12-27T10:45:10.137 回答
0

重新编辑:

采用:

取消报价

http://docs.python.org/2/library/urllib.html#urllib.unquote

或者

unquote_plus

http://docs.python.org/2/library/urllib.html#urllib.unquote_plus

用于转义 HTTP 类数据(即百分比转义)

有关更多信息,请参阅此问题和答案:

从 HTTP 转义 Python 字符串

并且,对于 unescape-in​​g 常规转义符号(即反斜杠),请使用 .decode()(与 .encode() 对应)。请参阅以下答案:

https://stackoverflow.com/a/10944959/1284631

https://stackoverflow.com/a/9340191/1284631

于 2012-12-27T10:41:14.247 回答