0

親子関係を通じてすべてが相互に関連するデータベース内の行の結果セットがあります

各行は objectid、id、parent、child、name、level のように表されるため、プログラムでデータベースから例を読み取ると、次のようになります

Organization1
Component1
Department1
Sections1
Sections2
Department2
Sections3
Component2
Department3
Sections4
Sections5
Department4
Sections6

組織には多くの部門があり、部門には多くのコンポーネントがあり、コンポーネントには多くのセクションがあります

これまでの私のコードはこのように見え、それは機能しますが、それをjson形式にする必要があり、json形式は以下のようにする必要があります

    for v in result:
        level = v[5]
        child = v[3]
        parent = v[2]
        if level == 0:
            OrgDic['InstID'] = v[4]
            OrgDic['Child'] = v[3]
            OrgDic['Parent'] = v[2]
            Organizations.append(InstDic)
            OrgDic = {}
        if level == 1:
            ComponentsDic['CollegeID'] = v[4]
            ComponentsDic['Child'] = v[3]
            ComponentsDic['Parent'] = v[2]
            Components.append(CollegeDic)
            ComponentsDic = {}
        if level == 2:
            DepartmentDic['DepartmentID'] = v[4]
            DepartmentDic['Child'] = v[3]
            DepartmentDic['Parent'] = v[2]
            Departments.append(DepartmentDic)
            DepartmentDic = {}
        if level == 3:
            SectionDic['SubjectID'] = v[4]
            SectionDic['Child'] = v[3]
            SectionDic['Parent'] = v[2]
            Sections.append(SubjectDic)
            SectionDic = {}


    for w in :
        print w['Organization']
        for x in Components:
            if w['Child'] == x['Parent']:
                print x['Components']
                for y in Departments:
                    if x['Child'] == y['Parent']:
                        print y['Deparments'] 
                        for z in Sections:
                            if y['Child'] == z['Parent']:
                                print z['Sections']

JSON形式

{
"Eff_Date": "08/02/2013",
"Tree":
[       
        {
            "OrganizationID": "Organization1",
            "Components":
            [
                {"ComponentID": "Component1",
                "Departments": 
                [
                    {"DepartmentID": "Dep1",
                    "Sections":
                    [
                        {"SectionID": "Section1"},
                        {"SectionID": "Section2"}
                    ]},
                    {"DepartmentID": "Dep2",
                    "Sections":
                    [
                        {"SectionID": "Section3"}
                    ]}
                ]}

]
}
4

2 に答える 2

0

以下の方法でできました

data[]
data.append([-1, 0 ,"name1", 0])
data.append([0,1, "name2", 1])
data.append([1, 2, "name3", 1])
data.append([2 ,3, "name4", 2])
data.append([2 ,4, "name5" ,2])
data.append([1 ,5, "name6", 2])
data.append([5, 6, "name7", 3])
data.append([5, 7, "name8",1])
data.append([5, 7, "name9",2])

def listToDict(input):
    root = {}
    lookup = {}
    for parent_id, id, name, attr in input:
        if parent_id == -1: 
            root['name'] = name;
            lookup[id] = root
        else:
            node = {'name': name}
            lookup[parent_id].setdefault('children', []).append(node)
            lookup[id] = node
    return root

result = listToDict(data)
print result
print json.dumps(result)

私の場合、データはデータベースからの結果セットだったので、次のようにループする必要がありました

    for v in result:
        values = [v[2], v[3], v[4], v[5]]
        pc.append(values)
于 2013-06-23T04:32:48.580 に答える