将字符串转换为JSON

4m1nh4j1

我想将此字符串转换为JSON字典:

{u'Processes': [[u'root', u'3606', u'0.0', u'0.2', u'76768', u'16664', u'?', u'Ss', u'20:40', u'0:01', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4088', u'0.0', u'0.2', u'88544', u'20156', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4090', u'0.0', u'0.2', u'88552', u'20140', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4097', u'0.0', u'0.2', u'88552', u'20112', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4110', u'0.0', u'0.2', u'88548', u'20160', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0']], u'Titles': [u'USER', u'PID', u'%CPU', u'%MEM', u'VSZ', u'RSS', u'TTY', u'STAT', u'START', u'TIME', u'COMMAND']}

所以我使用了json.dumps和json.loads,但是输出不是有效的JSON。我知道JSON需要双引号而不是简单的引号,但是我认为解决方案不是搜索和替换是解决此问题的最佳方法。有更合适的方法吗?

多甘·阿斯坎(Dogan Askan)

试试这个,

import json
data = {u'Processes': [[u'root', u'3606', u'0.0', u'0.2', u'76768', u'16664', u'?', u'Ss', u'20:40', u'0:01', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4088', u'0.0', u'0.2', u'88544', u'20156', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4090', u'0.0', u'0.2', u'88552', u'20140', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4097', u'0.0', u'0.2', u'88552', u'20112', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4110', u'0.0', u'0.2', u'88548', u'20160', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0']], u'Titles': [u'USER', u'PID', u'%CPU', u'%MEM', u'VSZ', u'RSS', u'TTY', u'STAT', u'START', u'TIME', u'COMMAND']}
data = json.dumps(data) # dict to string
data = json.loads(data) # string to json
print data['Processes']

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档