从JSON字符串中取消示例数据的任何聪明方法



我将带有celery后端的web2py服务器发送一些jsonrpc请求。有时,我会发现我想分析的错误。jsonrpc回复中逃脱了这些错误,因此不容易理解。我得到这样的东西:

{"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_jsonrpcn    s = methods[method](*params)n", "  File "/home/myuser1/tmp/web2py/applications/mycompany_portal/controllers/activity.py", line 66, in get_cdr_pagen    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_pagen    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 getn    interval=interval)n", "  File "/home/myuser1/.virtualenvs/python2.7.2-mycompany1/lib/python2.7/site-packages/celery/backends/amqp.py", line 138, in wait_forn    raise self.exception_to_python(meta['result'])n"], "name": "JSONRPCError"}}

我想要的是获取jsonrpcerror.data部分答复,取消示例并显示为堆栈Trace。我可以手动执行(更改"-> "并处理n),但我想避免在此处重新发明轮子。

这是原始的无与伦比的JSON吗?将其解析为JSON:

import json
print ''.join(json.loads(yourstring)['error']['data'])

重新编辑:

使用:

UnQuote

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

unquote_plus

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

用于Unescape-ing类似于HTTP的数据(即百分比删除)

有关更多信息,请参见此问题并答案:

从http

的unescape python字符串

和,对于unescape-ing strage under uscode(backslashes),请使用.decode()(.encode()的对方)。查看以下答案:

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

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

相关内容

  • 没有找到相关文章

最新更新