【问题标题】:Convert the output of openssl command to JSON将 openssl 命令的输出转换为 JSON
【发布时间】:2017-04-19 21:03:07
【问题描述】:

openssl 命令的输出如下所示:

serial=CABCSDUMMY4A168847703FGH
notAfter=Oct 21 16:43:47 2024 GMT
subject= /C=US/ST=WA/L=Seattle/O=MyCo/OU=TME/CN=MyCo.example.com

如何将此字符串转换为 JSON?

我试过这些:

temp_txt_bytes = subprocess.check_output (["openssl", "x509", "-serial", "-enddate", "-subject", "-noout", "-in", pem_file_name])

temp_txt_strings = temp_txt_bytes.decode("utf-8")

test = json.loads(temp_txt_strings) #json.parse, json.dump, and json.load also failing

【问题讨论】:

  • 使用json.dumps JSON。
  • 你要序列化什么?一串输出线?那只是json.dumps(temp_txt_strings)

标签: python json openssl


【解决方案1】:

你可以用“=”作为分隔符分割每一行,把这两部分放在一个有序的字典中,然后转储到json:

my_list = "serial=CABCSDUMMY4A168847703FGH".split("=")
ordered_dict = OrderedDict()
ordered_dict[my_list[0]] = my_list[1]
print(json.dumps(ordered_dict))

输出会是这样的:

{"serial": "CABCSDUMMY4A168847703FGH"}

您可以为所有线路执行此操作。 PS不要忘记导入json和OrderedDict

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-01
    • 2019-06-29
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    • 2017-02-04
    • 1970-01-01
    相关资源
    最近更新 更多