python中将多个数据生成嵌套的

原始数据

data = {
    "nodeDataArray": [
        {"category": "Start", "key": -1, "text": "start"},
        {"category": "Decision", "key": -9, "text": "jc-1", "value": "DS2021082018280100000000000001"},
        {"category": "Conditional", "key": -4, "text": "if"},
        {"category": "Decision", "key": -5, "text": "jc-2", "value": "DS2021082018281300000000000002"},
        {"category": "Conditional", "key": -6, "text": "if"},
        {"category": "Decision", "key": -7, "text": "jc-3", "value": "DS2021082018314100000000000003"},
        {"category": "Decision", "key": -8, "text": "jc-4", "value": "DS2021082417261100000000000004"},
        {"category": "Decision", "key": -10, "text": "jc-5", "value": "DS2021083109125200000000000005"}],
    "linkDataArray": [{"from": -1, "text": "", "to": -9},
                      {"from": -9, "text": "", "to": -4},
                      {"from": -4, "text": "符合", "to": -5, "visible": "true"},
                      {"from": -4, "text": "不符合", "to": -7, "visible": "true"},
                      {"from": -5, "text": "", "to": -6},
                      {"from": -6, "text": "符合", "to": -8, "visible": "true"},
                      {"from": -6, "text": "不符合", "to": -10, "visible": "true"}]
}

上面数据中key表示当前节点,from为to的前面一个节点。上面的key和下面的to是对应的

流程图如下

img

在linkDataArray中有的数据中是有"符合"和“不符合”的字样,通过上面数据生成一下的

{'DS2021082018280100000000000001': 'DS2021082018280100000000000001','if': {'true': {'DS2021082018281300000000000002': 'DS2021082018281300000000000002','if': {'true': {'DS2021082417261100000000000004': 'DS2021082417261100000000000004'},'false': {'DS2021083109125200000000000005': 'DS2021083109125200000000000005'}}},'false': {'DS2021082018314100000000000003': 'DS2021082018314100000000000003'}}}

其中为true的是在linkDataArray中符合的,false是对应的不符合,并且将嵌套到他的上一个节点from中,要是没有if判断的话就如下写法就好


{'DS2021082018280100000000000001': 'DS2021082018280100000000000001','DS2021082018281300000000000002':'DS2021082018281300000000000002'}

或者不按上面生成,生成比较易取,看起来舒服的一种数据格式也可以。

你题目的解答代码如下:(如有帮助,望采纳!谢谢! 点击我这个回答右上方的【采纳】按钮)

data = {
    "nodeDataArray": [
        {"category": "Start", "key": -1, "text": "start"},
        {"category": "Decision", "key": -9, "text": "jc-1", "value": "DS2021082018280100000000000001"},
        {"category": "Conditional", "key": -4, "text": "if"},
        {"category": "Decision", "key": -5, "text": "jc-2", "value": "DS2021082018281300000000000002"},
        {"category": "Conditional", "key": -6, "text": "if"},
        {"category": "Decision", "key": -7, "text": "jc-3", "value": "DS2021082018314100000000000003"},
        {"category": "Decision", "key": -8, "text": "jc-4", "value": "DS2021082417261100000000000004"},
        {"category": "Decision", "key": -10, "text": "jc-5", "value": "DS2021083109125200000000000005"}],
    "linkDataArray": [{"from": -1, "text": "", "to": -9},
                      {"from": -9, "text": "", "to": -4},
                      {"from": -4, "text": "符合", "to": -5, "visible": "true"},
                      {"from": -4, "text": "不符合", "to": -7, "visible": "true"},
                      {"from": -5, "text": "", "to": -6},
                      {"from": -6, "text": "符合", "to": -8, "visible": "true"},
                      {"from": -6, "text": "不符合", "to": -10, "visible": "true"}]
}

dd = {x["key"]:x for x in data["nodeDataArray"]}
start = list(filter(lambda x: x["category"]=="Start", data["nodeDataArray"]))[0]["key"]
start1 = list(filter(lambda x: x["from"]==start, data["linkDataArray"]))[0]["to"]
def jnp(s,dic):
    li = [x for x in data["linkDataArray"] if x["from"]==s]
    if dd[s]["category"]=="Conditional":
        r = {}
        dic[dd[s]["text"]] = r
        for v in li:
            t = {}
            r[str(v["text"]=="符合")] = t
            jnp(v['to'],t)
    else:
        dic[dd[s]["value"]] = dd[s]["value"]
        for v in li:
            jnp(v['to'],dic)

res = {}
jnp(start1,res)
print(res)

没太看懂是要干啥 另外json是“”,不是单引号,在线json都识别不了哦