无法解析此json文件

时间:2018-06-07 02:04:04

标签: python json parsing

我有一个类似的json文件:

[
{
  "host": "host1.com",
  "ip": "x.x.x.x",
  "port": 443,
  "tlsVersions": [
    "TLSv1_2"
  ],
  "cipherSuite": {
    "supported": [
      "ECDHE-RSA-AES256-GCM-SHA384"
    ]
  },
  "x509ChainDepth": 2,
  "verifyCertResult": true,
  "verifyHostResult": true,
  "ocspStapled": true,
  "verifyOcspResult": true,
  "certificateChain": [
  {
    "version": 3
  },  {
    "version": 3
  } ]
},

{
  "host": "host2.com",
  "ip": "y.y.y.y",
  "port": 443,
  "tlsVersions": [
    "TLSv1_2"
  ],
  "cipherSuite": {
    "supported": [
      "ECDHE-RSA-AES256-GCM-SHA384"
    ]
  },
  "x509ChainDepth": 2,
  "verifyCertResult": true,
  "verifyHostResult": true,
  "ocspStapled": true,
  "verifyOcspResult": true,
  "certificateChain": [
  {
    "version": 3
  },  {
    "version": 3
  } ]
}

]

我想为每个主机提取hosttlsVersions,然后将它们打印在一个用逗号分隔的行中。我想循环遍历每个主机并打印一个host,每行tlsVersions

我试过了:

import json 

with open('result2.json', 'r') as f:
    distros_dict = json.load(f)

for distro in distros_dict:
    print(distro['host']+","+distro['tlsVersions']+'\n')

运行脚本时出现此错误:

Traceback (most recent call last):
  File "parser.py", line 4, in <module>
    distros_dict = json.load(f)
  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

有什么问题?安装了json库,并且import语句可以正常工作。

4 个答案:

答案 0 :(得分:0)

您的对象上有逗号逗号。很多JSON解析器都会遇到这种情况。

而不是:

{
  "foo": "bar",
}

你需要更像这样的东西:

{
  "foo": "bar"
}

答案 1 :(得分:0)

如果不是关于尾随逗号的答案,请尝试此操作。

我认为它期望JSON数据包含一个对象,你有一个数组。

你有什么:

[ 
  { ... }, 
  { ... }
]

我认为它的期望:

{
  "Items": [
    { ... }, 
    { ... }
  ]
}

答案 2 :(得分:0)

问题是distro['tlsVersions']是一个数组,而不是一个字符串。

相比之下,以下修改是有效的,尽管它可能不是您真正想要的:

for distro in distros_dict:
    print(distro['host']+","+distro['tlsVersions'][0]+'\n')

以上产生:

host1.com,TLSv1_2

host2.com,TLSv1_2

答案 3 :(得分:0)

@ user9371654 ,您的JSON filePython code存在两个主要问题。

  • result2.json的内容格式不正确。

  • print()函数的参数应与,分开,因此将用于连接的所有+替换为,(如果您仍然喜欢,然后你需要使用str()将列表转换为字符串以支持连接。

  

复制result2.json的JSON内容,并将其格式化为https://jsonformatter.curiousconcept.com

enter image description here

result2.json»

enter image description here

  

注意:使用以下内容更新您的JSON文件result2.json

[  
   {  
      "host":"host1.com",
      "ip":"x.x.x.x",
      "port":443,
      "tlsVersions":[  
         "TLSv1_2"
      ],
      "cipherSuite":{  
         "supported":[  
            "ECDHE-RSA-AES256-GCM-SHA384"
         ]
      },
      "x509ChainDepth":2,
      "verifyCertResult":true,
      "verifyHostResult":true,
      "ocspStapled":true,
      "verifyOcspResult":true,
      "certificateChain":[  
         {  
            "version":3
         },
         {  
            "version":3
         }
      ]
   },
   {  
      "host":"host2.com",
      "ip":"y.y.y.y",
      "port":443,
      "tlsVersions":[  
         "TLSv1_2"
      ],
      "cipherSuite":{  
         "supported":[  
            "ECDHE-RSA-AES256-GCM-SHA384"
         ]
      },
      "x509ChainDepth":2,
      "verifyCertResult":true,
      "verifyHostResult":true,
      "ocspStapled":true,
      "verifyOcspResult":true,
      "certificateChain":[  
         {  
            "version":3
         },
         {  
            "version":3
         }
      ]
   }
]

Python代码»

最后尝试以下代码:

import json 

with open('result2.json', 'r') as f:
    distros_dict = json.load(f)

for distro in distros_dict:
    print(distro['host'], "," , distro['tlsVersions'], '\n')

输出»

host1.com , ['TLSv1_2']

host2.com , ['TLSv1_2']