- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要一些关于一个简单问题的帮助。
我正在尝试从此转换 json 文件的内容:
{ "Timestamp": "Timestamp",
"name": "SVCHOST.EXE",
"icon": "binary_icon.png",
"Process": SVCHOST.EXE,
"Pid": "876",
"PPID": "500"],
"children": [Process details])
},
{ "Timestamp":"Timestamp",
"name": "LSAS.EXE",
"icon": "binary_icon.png",
"Process": "LSAS.EXE",
"Pid": "500",
"PPID": "4"],
"children": [Process details])
},
{ "Timestamp":"Timestamp",
"name": "SYSTEM",
"icon": "binary_icon.png",
"Process": "SYSTEM",
"Pid": "4",
"PPID": "0"],
"children": [Process details])
}
对此:
{
"name": "Root",
"children": [
{
"name": "4",
"children": [
{
"name": "500",
"children": [
{
"name": "876",
"children": []
}
]
}
]
}
}
最后创建一个节点 TreeMap 。
但是经过大量的试验和错误,仍然没有接近我需要的输出。我正在寻求一些指导、提示或技巧。非常感谢任何帮助。
谢谢
这是我最近的尝试。
import json
links = ({
"Timestamp": "Timestamp",
"name": "SVCHOST.EXE",
"icon": "binary_icon.png",
"Process": "SVCHOST.EXE",
"Pid": "876",
"PPID": "500",
"children": "Process_details"
},
{
"Timestamp":"Timestamp",
"name": "LSAS.EXE",
"icon": "binary_icon.png",
"Process": "LSAS.EXE",
"Pid": "500",
"PPID": "4",
"children": "Process_details"
},
{
"Timestamp":"Timestamp",
"name": "SYSTEM",
"icon": "binary_icon.png",
"Process": "SYSTEM",
"Pid": "4",
"PPID": "0",
"children": "Process_details"
})
parent_proc_node = {}
root = {'name': 'Root', 'children': []}
for item in procs:
parent_node = parent_proc_node.get(item['Pid'])
if not parent_node:
parent_proc_node[item['Pid']] = parent_node = {'name': item['PPID']}
root['children'].append(parent_node)
parent_proc_node[item['PPID']] = child_node = {'name': item['Pid']}
parent_node.setdefault('children', []).append(child_node)
print json.dumps(root, indent=4)
当前输出:
{
"name": "Root",
"children": [
{
"name": "500",
"children": [
{
"name": "876",
"children": [
{
"name": "500",
"children": [
{
"name": "4"
}
]
}
]
}
]
}
}
现在的输出是我想要的,但我仍然无法正确地将父进程与子进程匹配。我做错了什么?
正确的输出应该是这样的:
{
"name": "Root",
"children": [
{
"name": "4",
"children": [
{
"name": "500",
"children": [
{
"name": "876",
"children": [
{
"name": ""
}
]
}
]
}
]
}
}
最佳答案
这里有一些代码可以实现我认为您想要的功能。它处理链接
(我将其转换为列表,因为 JSON 没有元组),将其转换为嵌套结构,您将其显示为最终正确的输出。我还添加了一些新记录,以便一些 parent 有多个 child 。
技巧是首先创建一个字典 (ids
) 来捕获进程 ID 的父子关系。
import json
links = [
{
"Timestamp": "Timestamp",
"name": "SVCHOST.EXE",
"icon": "binary_icon.png",
"Process": "SVCHOST.EXE",
"Pid": "876",
"PPID": "500",
"children": "Process_details"
},
{
"Timestamp": "Timestamp",
"name": "LSAS.EXE",
"icon": "binary_icon.png",
"Process": "LSAS.EXE",
"Pid": "500",
"PPID": "4",
"children": "Process_details"
},
{
"Timestamp": "Timestamp",
"name": "LSAS.EXE",
"icon": "binary_icon.png",
"Process": "LSAS.EXE",
"Pid": "510",
"PPID": "4",
"children": "Process_details"
},
{
"Timestamp": "Timestamp",
"name": "LSAS.EXE",
"icon": "binary_icon.png",
"Process": "LSAS.EXE",
"Pid": "600",
"PPID": "510",
"children": "Process_details"
},
{
"Timestamp": "Timestamp",
"name": "SYSTEM",
"icon": "binary_icon.png",
"Process": "SYSTEM",
"Pid": "4",
"PPID": "0",
"children": "Process_details"
}
]
# Create a dict linking each pid to its parent
ids = {}
for d in links:
# Use "0" as the ppid if "PPID" field is an empty string
ppid, pid = d["PPID"] or "0", d["Pid"]
ids.setdefault(ppid, []).append(pid)
print(ids)
# Nest the data for each pid in its parent's dict
def insert(lst, ppid, name):
if ppid in ids:
children = []
lst.append({"name": name, "children": children})
for pid in ids[ppid]:
insert(children, pid, pid)
else:
children = [{"name": ""}]
lst.append({"name": name, "children": children})
nested = []
insert(nested, "0", "Root")
print(json.dumps(nested[0], indent=4))
输出
{'500': ['876'], '4': ['500', '510'], '510': ['600'], '0': ['4']}
{
"name": "Root",
"children": [
{
"name": "4",
"children": [
{
"name": "500",
"children": [
{
"name": "876",
"children": [
{
"name": ""
}
]
}
]
},
{
"name": "510",
"children": [
{
"name": "600",
"children": [
{
"name": ""
}
]
}
]
}
]
}
]
}
关于python - 创建流程树形式的 json 文件。在Python中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51971224/
是否有办法获取所选 TreeView 节点的索引,或者他们是否有一个? 最佳答案 由于您要求“索引”只是为了能够查找与此项目关联的数据,因此您应该知道树控件可以保存您的数据。每个项目(TVITEM 结
我不太确定这个问题以前是否有人问过,因为它似乎太微不足道了,不太新鲜,但是我已经环顾四周一个小时了,还没有发现任何东西 我正在使用 ttk Treeview 来显示和分析表格。对于给定的函数,我希望能
我不太确定这个问题以前是否有人问过,因为它似乎太微不足道了,不太新鲜,但是我已经环顾四周一个小时了,还没有发现任何东西 我正在使用 ttk Treeview 来显示和分析表格。对于给定的函数,我希望能
我有一个复杂的 json 字符串,如下所示: { "id":"2016666", "dt":"2012", "object_extends":[ {
我用了JQuery TreeView在大模式下,我想折叠除 root 之外的所有项目,如何更改 jquery.treeview.js 来执行此操作? 最佳答案 嗯..也许不是最干净的解决方案,但似乎有
这听起来可能很奇怪,因为我无法找到我想做的确切术语。 我正在开发一个应用程序,它具有一组规则(易于转换为函数)和输入/输出对(不那么容易转换为代码),将允许构造规则树以应用于给定的输入达到给定的输出。
我是网络开发的菜鸟。我正在尝试创建一个树状的分层公司组织结构图。我尝试了两个谷歌的 visualization chart和 Mike Bostock 的 D3 Reingold tree . 我想要
我是一名优秀的程序员,十分优秀!