解析这个嵌套的 json?
Parsing this nested json?
我正在尝试解析从 xml 转换为 json 的 nmap 扫描结果。 json 我无法提取的部分内容如下:
"ports": {
"port": {
"@portid": "22",
"state": {
"@state": "open",
},
"service": {
"@product": "OpenSSH",
},
"script": [
{
"@output": "\n ssh-dss AAAArO379Amw==\n ssh-rsa AAAAB3nSb3vZeQAw==",
"@id": "ssh-hostkey",
"table": [
{
"elem": [
{
"@key": "key",
"#tail": "\n",
"#text": "QUFBQUIzTnphQzFrYzNNQUFBQ0JBUHkzQVhaemI4N1IzRvQnh3ZWlOaJtLzc1azBsMGtVMG1ock8zNzlBbXc9PQ=="
},
{
"@key": "fingerprint",
"#tail": "\n",
"#text": "4724ad3b5f2"
}
],
"#tail": "\n",
"#text": "\n"
},
{
"elem": [
{
"@key": "key",
"#tail": "\n",
"#text": "QUFBQUIzTnphQzF5YzJFQUFBQUJJd0FBQVFFQXJlZTc5cWhlNUJxQ3RPWElYbDVrbTY0azI5eHR3UStyTnRiM3ZaZVFBdz09"
},
{
"@key": "fingerprint",
"#tail": "\n",
"#text": "a015719c9"
}
],
"#tail": "\n",
"#text": "\n"
}
]
}}}
我正在尝试从脚本键中提取部分,我的代码如下所示
if 'script' in extracted_json['ports']['port']:
for something in extracted_json['ports']['port']['script']['table']:
print something['elem']
然而,当我 运行 它时,我收到错误消息 TypeError: string indices must be integers
。如果我尝试将 print
语句更改为 print something[1]
,那么我会得到:KeyError: 1
。我应该如何从这个嵌套的 json?
中提取信息
变量某物包含这个
{
u '#tail': u '\n', u '#text': u '\n', u 'elem': [{
u '#tail': u '\n',
u '@key': u 'key',
u '#text': u 'QUFBQUIzTnTVaWURiMFNpdz09'
}, {
u '#tail': u '\n',
u '@key': u 'bits',
u '#text': u '2048'
}, {
u '#tail': u '\n',
u '@key': u 'fingerprint',
u '#text': u '239d68c0c083446c'
}, {
u '#tail': u '\n',
u '@key': u 'type',
u '#text': u 'ssh-rsa'
}]
}
script
包含一个数组,需要循环遍历。而table
也是一个数组。
for script in extracted_json['ports']['port']['script']:
for table in script['table']:
print table['elem']
我正在尝试解析从 xml 转换为 json 的 nmap 扫描结果。 json 我无法提取的部分内容如下:
"ports": {
"port": {
"@portid": "22",
"state": {
"@state": "open",
},
"service": {
"@product": "OpenSSH",
},
"script": [
{
"@output": "\n ssh-dss AAAArO379Amw==\n ssh-rsa AAAAB3nSb3vZeQAw==",
"@id": "ssh-hostkey",
"table": [
{
"elem": [
{
"@key": "key",
"#tail": "\n",
"#text": "QUFBQUIzTnphQzFrYzNNQUFBQ0JBUHkzQVhaemI4N1IzRvQnh3ZWlOaJtLzc1azBsMGtVMG1ock8zNzlBbXc9PQ=="
},
{
"@key": "fingerprint",
"#tail": "\n",
"#text": "4724ad3b5f2"
}
],
"#tail": "\n",
"#text": "\n"
},
{
"elem": [
{
"@key": "key",
"#tail": "\n",
"#text": "QUFBQUIzTnphQzF5YzJFQUFBQUJJd0FBQVFFQXJlZTc5cWhlNUJxQ3RPWElYbDVrbTY0azI5eHR3UStyTnRiM3ZaZVFBdz09"
},
{
"@key": "fingerprint",
"#tail": "\n",
"#text": "a015719c9"
}
],
"#tail": "\n",
"#text": "\n"
}
]
}}}
我正在尝试从脚本键中提取部分,我的代码如下所示
if 'script' in extracted_json['ports']['port']:
for something in extracted_json['ports']['port']['script']['table']:
print something['elem']
然而,当我 运行 它时,我收到错误消息 TypeError: string indices must be integers
。如果我尝试将 print
语句更改为 print something[1]
,那么我会得到:KeyError: 1
。我应该如何从这个嵌套的 json?
变量某物包含这个
{
u '#tail': u '\n', u '#text': u '\n', u 'elem': [{
u '#tail': u '\n',
u '@key': u 'key',
u '#text': u 'QUFBQUIzTnTVaWURiMFNpdz09'
}, {
u '#tail': u '\n',
u '@key': u 'bits',
u '#text': u '2048'
}, {
u '#tail': u '\n',
u '@key': u 'fingerprint',
u '#text': u '239d68c0c083446c'
}, {
u '#tail': u '\n',
u '@key': u 'type',
u '#text': u 'ssh-rsa'
}]
}
script
包含一个数组,需要循环遍历。而table
也是一个数组。
for script in extracted_json['ports']['port']['script']:
for table in script['table']:
print table['elem']