Pyqt4如何解析无限分级的json文件,并显示在QTreeWidget中?

BryantKunZhe
  • 7

json数据的格式如下
[
{

"name": "曾庆海",
"birthday": "1885-4-25",
"address": "湖南长沙",
"marry": "是",
"alive": "否",
"deathday": "1975-3-29",
"children": [
  {
    "name": "曾鹏",
    "birthday": "1910-3-31",
    "address": "湖南长沙",
    "marry": "是",
    "alive": "否",
    "deathday": "2000-6-28",
    "children": [
      {
        "name": "曾飞",
        "birthday": "1935-10-6",
        "address": "江西南昌",
        "marry": "是",
        "alive": "是",
        "deathday": "- - -",
        "children": []
      },
      {
        "name": "曾路",
        "birthday": "1937-7-28",
        "address": "江西南昌",
        "marry": "是",
        "alive": "是",
        "deathday": "- - -",
        "children": []
      }
    ]
  },
  {
    "name": "曾勤",
    "birthday": "1912-8-22",
    "address": "湖南长沙",
    "marry": "是",
    "alive": "否",
    "deathday": "2001-8-23",
    "children": [
      {
        "name": "曾海",
        "birthday": "1936-10-2",
        "address": "浙江杭州",
        "marry": "是",
        "alive": "是",
        "deathday": "- - -",
        "children": []
      },
      {
        "name": "曾树",
        "birthday": "1938-2-16",
        "address": "江苏南京",
        "marry": "是",
        "alive": "是",
        "deathday": "- - -",
        "children": []
      }
    ]
  },
  {
    "name": "曾勇",
    "birthday": "1915-11-26",
    "address": "湖南长沙",
    "marry": "是",
    "alive": "否",
    "deathday": "2002-2-21",
    "children": [
      {
        "name": "曾强",
        "birthday": "1940-4-18",
        "address": "河南洛阳",
        "marry": "是",
        "alive": "是",
        "deathday": "- - -",
        "children": []
      },
      {
        "name": "曾禄",
        "birthday": "1942-8-18",
        "address": "河南洛阳",
        "marry": "是",
        "alive": "是",
        "deathday": "- - -",
        "children": []
      }
    ]
  }
]

}
]
这些数据是我从QTreeWidget中提取出来保存在json文件中,现在我想json文件重新解析生成Pyqt中的树状视图

clipboard.png

(图片只显示上面的部分数据)
不知道有什么好的方法?

回复
阅读 3.3k
1 个回答

创建两个类

-- coding: utf-8 --

class FamilyNode:

def __init__(self, identify = None, parentId = None, 
    children = None, nodeInfo = None):
    self.identify = identify
    self.parentId = parentId
    self.children = children
    self.nodeInfo = nodeInfo

-- coding: utf-8 --

class PersonInfo:

def __init__(self, name = None, birthday = None, address = None,
 marry = None, alive = None, deathday = None):
    self.name = name
    self.birthday = birthday
    self.address = address
    self.marry = marry
    self.alive = alive
    self.deathday = deathday

利用队列先进先出的特性
import json
import queue
from PersonInfo import PersonInfo
from FamilyNode import FamilyNode

with open("./家谱文件/1.json", encoding = "utf-8") as f:

content = json.load(f)

jsonData = content[0]
indentify = 1
root = FamilyNode()
root.nodeInfo = PersonInfo()
root.nodeInfo.name = jsonData["name"]
root.nodeInfo.birthday = jsonData["birthday"]
root.nodeInfo.address = jsonData["address"]
root.nodeInfo.marry = jsonData["marry"]
root.nodeInfo.alive = jsonData["alive"]
root.nodeInfo.deathday = jsonData["deathday"]
root.children = jsonData["children"]
root.indentify = indentify
root.parentId = 0

itemList = []
itemQueue = queue.Queue()
itemQueue.put(root)

while itemQueue.empty() == False:

node = itemQueue.get()
itemList.append(node)
nodeList = node.children
for item in nodeList:
    child = FamilyNode()
    child.nodeInfo = PersonInfo()
    indentify = indentify + 1
    child.nodeInfo.name = item["name"]
    child.nodeInfo.birthday = item["birthday"]
    child.nodeInfo.address = item["address"]
    child.nodeInfo.marry = item["marry"]
    child.nodeInfo.alive = item["alive"]
    child.nodeInfo.deathday = item["deathday"]
    child.children = item["children"]
    child.indentify = indentify
    child.parentId = node.indentify
    itemQueue.put(child)

itemList中存放着新的数据结构

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏