繁体   English   中英

无法解析此json文件

[英]Can't parse this json file

我有一个看起来像的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并将它们打印在用逗号分隔的行中。 我想遍历每台主机,并用每行tlsVersions打印一台host

我尝试了这个:

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语句可以正常运行。

您的对象上有逗号结尾。 许多JSON解析器都会为此而苦苦挣扎。

而不是这样:

{
  "foo": "bar",
}

您需要这样的东西:

{
  "foo": "bar"
}

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

我认为期望JSON数据包含一个具有数组的对象。

你有什么:

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

我认为这是期望的:

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

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

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

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

上面产生:

host1.com,TLSv1_2

host2.com,TLSv1_2

@ user9371654 ,您的JSON filePython code有2个主要问题。

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

  • 的参数print()函数应该分开,所以更换所有+用于级联用, (如果你还是喜欢,那么uou需要使用到您的列表转换为字符串str()来支持级联)。

复制您的result2.json的JSON内容,并在https://jsonformatter.curiousconcept.com对其进行格式化。

在此处输入图片说明

result2.json»

在此处输入图片说明

注意:使用以下内容更新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']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM