如何在Python中的嵌套json中替换键中的字符
"127.0.0.1": {
"nmaprun": {
"@scanner": "nmap",
"@args": "nmap -v -sS -sV -sC -A -O -oX nmap 127.0.0.1 1-1024",
"@start": "1645467733",
"@startstr": "Mon Feb 21 23:52:13 2022",
"@version": "7.91",
"@xmloutputversion": "1.05",
"scaninfo": {
"@type": "syn",
"@protocol": "tcp",
"@numservices": "1000",
"@services": "1,3-4,6-7,9,13,17,19-26,
},
"verbose": {
"@level": "1"
},
"debugging": {
"@level": "0"
},
"runstats": {
"finished": {
"@time": "1645467744",
"@timestr": "Mon Feb 21 23:52:24 2022",
"@summary": "Nmap done at Mon Feb 21 23:52:24 2022; 1 IP address (1 host up) scanned in 12.14 seconds",
"@elapsed": "12.14",
"@exit": "success"
}
}
}
}
}
我有 Nmap 的扫描输出。我想解析整个 JSON 并将“@”字符替换为“”。 我怎样才能在Python中做到这一点?
"127.0.0.1": {
"nmaprun": {
"@scanner": "nmap",
"@args": "nmap -v -sS -sV -sC -A -O -oX nmap 127.0.0.1 1-1024",
"@start": "1645467733",
"@startstr": "Mon Feb 21 23:52:13 2022",
"@version": "7.91",
"@xmloutputversion": "1.05",
"scaninfo": {
"@type": "syn",
"@protocol": "tcp",
"@numservices": "1000",
"@services": "1,3-4,6-7,9,13,17,19-26,
},
"verbose": {
"@level": "1"
},
"debugging": {
"@level": "0"
},
"runstats": {
"finished": {
"@time": "1645467744",
"@timestr": "Mon Feb 21 23:52:24 2022",
"@summary": "Nmap done at Mon Feb 21 23:52:24 2022; 1 IP address (1 host up) scanned in 12.14 seconds",
"@elapsed": "12.14",
"@exit": "success"
}
}
}
}
}
I have this scan output from Nmap. I want to parse the entire JSON and replace the '@' character with ''.
how can I do this in python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种可能的方法是迭代键,然后弹出给定键的项目并将其分配给不带@的键,如果键以
@
开头,因为您有嵌套结构,所以您需要递归地遍历这些键输出:
One possible way is to iterate the keys, and then to pop the item for the given key and assign it to the key without @, if the key starts with
@
, since you have nested structure, you need to go through these keys recursivelyOUTPUT: