无法访问文本文件中的JSON数据

时间:2018-08-14 21:14:44

标签: python json

下面是文本文件中的JSON数据。

{u'jsonrpc': u'2.0', u'result': [{u'status': u'3', u'name': u'Linux operating system', u'triggers': [{u'triggerid': u'10010'}, {u'triggerid': u'10011'}, {u'triggerid': u'10012'}, {u'triggerid': u'10016'}, {u'triggerid': u'10021'}, {u'triggerid': u'10041'}, {u'triggerid': u'10042'}, {u'triggerid': u'10043'}, {u'triggerid': u'10044'}, {u'triggerid': u'10045'}, {u'triggerid': u'10047'}, {u'triggerid': u'10190'}, {u'triggerid': u'13000'}, {u'triggerid': u'13243'}, {u'triggerid': u'13508'}, {u'triggerid': u'17316'}, {u'triggerid': u'17317'}, {u'triggerid': u'17318'}, {u'triggerid': u'17319'}, {u'triggerid': u'17320'}], u'items': [{u'name': u'Host name of apache running'}, {u'name': u'local ping'}, {u'name': u'Version of apache-agent(d) running'}, {u'name': u'Maximum number of opened files'}, {u'name': u'Maximum number of processes'}, {u'name': u'Number of running processes'}, {u'name': u'Number of processes'}, {u'name': u'Host boot time'}, {u'name': u'calls per second'}, {u'name': u'Processor load (2 min average per core)'}, {u'name': u'Processor load (15 min average per core)'}, {u'name': u'Processor load (5 min average per core)'}, {u'name': u'Context switches per second'}, {u'name': u'CPU $2 time'}, {u'name': u'CPU $2 time'}, {u'name': u'CPU $2 time'}, {u'name': u'CPU $2 time'}, {u'name': u'CPU $2 time'}, {u'name': u'CPU $2 time'}, {u'name': u'CPU $2 time'}, {u'name': u'CPU $2 time'}, {u'name': u'Host name'}, {u'name': u'Host local time'}, {u'name': u'Free swap space'}, {u'name': u'Free swap space in %'}, {u'name': u'Total swap space'}, {u'name': u'System information'}

我试图从文本文件中读取JSON数据并获取所有名称键。

with open("convergedcloudDetailedOutput.txt", "r") as f:
      cc_template_content = json.loads(f)


for ccresult in cc_template_content['result']:
       print (ccresult['name'])

但是当我运行代码时,我遇到了错误。任何帮助,将不胜感激。我尝试了谷歌的所有建议,但没有一个奏效。

       Traceback (most recent call last):
  File "automatedTempalte.py", line 45, in <module>
    cc_template_content = json.loads(f)
  File "/usr/local/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer

2 个答案:

答案 0 :(得分:0)

在我看来,这似乎不是有效的json ...

{"jsonrpc": "2.0", "result": [{"status": "3", "name": "Linux operating system", "triggers": [{"triggerid": "10010"}, {"triggerid": "10011"}, {"triggerid": "10012"}, {"triggerid": "10016"}, {"triggerid": "10021"}, {"triggerid": "10041"}, {"triggerid": "10042"}, {"triggerid": "10043"}, {"triggerid": "10044"}, {"triggerid": "10045"}, {"triggerid": "10047"}, {"triggerid": "10190"}, {"triggerid": "13000"}, {"triggerid": "13243"}, {"triggerid": "13508"}, {"triggerid": "17316"}, {"triggerid": "17317"}, {"triggerid": "17318"}, {"triggerid": "17319"}, {"triggerid": "17320"}], "items": [{"name": "Host name of apache running"}, {"name": "local ping"}, {"name": "Version of apache-agent(d) running"}, {"name": "Maximum number of opened files"}, {"name": "Maximum number of processes"}, {"name": "Number of running processes"}, {"name": "Number of processes"}, {"name": "Host boot time"}, {"name": "calls per second"}, {"name": "Processor load (2 min average per core)"}, {"name": "Processor load (15 min average per core)"}, {"name": "Processor load (5 min average per core)"}, {"name": "Context switches per second"}, {"name": "CPU $2 time"}, {"name": "CPU $2 time"}, {"name": "CPU $2 time"}, {"name": "CPU $2 time"}, {"name": "CPU $2 time"}, {"name": "CPU $2 time"}, {"name": "CPU $2 time"}, {"name": "CPU $2 time"}, {"name": "Host name"}, {"name": "Host local time"}, {"name": "Free swap space"}, {"name": "Free swap space in %"}, {"name": "Total swap space"}, {"name": "System information"}]}]}

尝试测试一下...

答案 1 :(得分:0)

open("convergedcloudDetailedOutput.txt", "r") 

此语句返回一个对象。您想使用f.read()访问该字符串。