无法解析json文件,提示无法解码为JSON对象

2024-04-20 07:06:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个json转储

{
  "alarm": [
    {
      "ackId": 16,
      "count": 1,
      "description": "<p>A SSH outage was identified on interface\n      10.21.5.39.</p> <p>A new Outage record has been\n      created and service level availability calculations will be\n      impacted until this outage is resolved.</p>",
      "firstEventTime": 1495277308427,
      "id": 16,
      "ifIndex": null,
      "ipAddress": "10.21.5.39",
      "lastEvent": {
        "createTime": 1495277308437,
        "description": "<p>A SSH outage was identified on interface\n      10.21.5.39.</p> <p>A new Outage record has been\n      created and service level availability calculations will be\n      impacted until this outage is resolved.</p>",
        "display": "Y",
        "host": "opennms",
        "id": 625,
        "ifIndex": null,
        "ipAddress": "10.21.5.39",
        "log": "Y",
        "logMessage": "SSH outage identified on interface 10.21.5.39 with reason code: Connection refused (Connection refused).",
        "nodeId": 9,
        "nodeLabel": "fra01-api-01",
        "parameters": [
          {
            "name": "eventReason",
            "type": "string",
            "value": "Connection refused (Connection refused)"
          }
        ],
        "serviceType": {
          "id": 5,
          "name": "SSH"
        },
        "severity": "MINOR",
        "source": "OpenNMS.Poller.DefaultPollContext",
        "time": 1495277308427,
        "uei": "uei.opennms.org/nodes/nodeLostService"
      },
      "lastEventTime": 1495277308427,
      "logMessage": "SSH outage identified on interface 10.21.5.39 with reason code: Connection refused (Connection refused).",
      "managedObjectInstance": null,
      "managedObjectType": null,
      "nodeId": 9,
      "nodeLabel": "fra01-api-01",
      "ossPrimaryKey": null,
      "parameters": [
        {
          "name": "eventReason",
          "type": "string",
          "value": "Connection refused (Connection refused)"
        }
      ],
      "qosAlarmState": null,
      "reductionKey": "uei.opennms.org/nodes/nodeLostService::9:10.21.5.39:SSH",
      "serviceType": {
        "id": 5,
        "name": "SSH"
      },
      "severity": "MINOR",
      "suppressedTime": 1495277308427,
      "suppressedUntil": 1495277308427,
      "type": 1,
      "uei": "uei.opennms.org/nodes/nodeLostService",
      "x733AlarmType": null,
      "x733ProbableCause": 0
    }
  ],
  "count": 1,
  "offset": null,
  "totalCount": 1
}

我编写了一个小代码来获取json的一些细节

^{pr2}$

这是在给我一个错误:

Traceback (most recent call last): File "python_opennms.py", line 24, in x = get_nodes_opennms() File "python_opennms.py", line 15, in get_nodes_opennms for i in json.load(open('out.txt'))["alarm"]: File "/usr/lib/python2.7/json/init.py", line 291, in load **kw) File "/usr/lib/python2.7/json/init.py", line 339, in loads return _default_decoder.decode(s) File "/usr/lib/python2.7/json/decoder.py", line 364, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded

有人能帮忙吗


Tags: inpyjsononlineconnectionnullssh
1条回答
网友
1楼 · 发布于 2024-04-20 07:06:57

您正在打开写入后未关闭的文件。可能是系统没有将流缓冲区完全刷新到磁盘,所以open('out.txt')遇到了内容不完整的文件。在

最好和最安全的方法是:

with open('out.txt', 'w') as f:
    f.write(wriet_me_to_file)
for i in json.load(open('out.txt'))["alarm"]:

相关问题 更多 >