In [37]: s = "{'osFreeMem': 286494720, 'osUpTime': 19230718, 'sysload': [0.24609375, 0.62109375, 0.734375], 'nodeUpTime': 1831, 'osTotMem': 536870912, 'nodeMemory': {'heapTotal': 8766304, 'heapUsed': 7789588, 'rss': 22773760}}"
In [38]: import ast
In [39]: d = ast.literal_eval(s)
In [40]: d
Out[40]:
{'nodeMemory': {'heapTotal': 8766304, 'heapUsed': 7789588, 'rss': 22773760},
'nodeUpTime': 1831,
'osFreeMem': 286494720,
'osTotMem': 536870912,
'osUpTime': 19230718,
'sysload': [0.24609375, 0.62109375, 0.734375]}
In [41]: d.items()
Out[41]:
[('osFreeMem', 286494720),
('osUpTime', 19230718),
('sysload', [0.24609375, 0.62109375, 0.734375]),
('nodeUpTime', 1831),
('osTotMem', 536870912),
('nodeMemory', {'heapTotal': 8766304, 'heapUsed': 7789588, 'rss': 22773760})]
或
In [44]: import json
In [45]: json.loads(s.replace("'",'"')).items()
Out[45]:
[(u'osFreeMem', 286494720),
(u'osUpTime', 19230718),
(u'sysload', [0.24609375, 0.62109375, 0.734375]),
(u'nodeUpTime', 1831),
(u'osTotMem', 536870912),
(u'nodeMemory', {u'heapTotal': 8766304, u'heapUsed': 7789588, u'rss': 22773760})]
你不要任何理由要使用JSON解析器? – 2013-03-12 12:23:47
你從哪裏得到那個字符串?你可能直接獲取數據。如果您無法直接獲取數據,我會將單引號切換爲double並將其解析爲JSON。 – 2013-03-12 12:24:09
我通過websocket從服務器獲取它。我怎樣才能解析它與JSON PARSER? – 2013-03-12 12:26:29