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

发布于 2022-09-06 00:21:09 字数 2115 浏览 7 评论 0

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒废 2022-09-13 00:21:09

创建两个类

-- 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中存放着新的数据结构

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文