Pyqt4如何解析无限分级的json文件,并显示在QTreeWidget中?
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中的树状视图
(图片只显示上面的部分数据)
不知道有什么好的方法?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建两个类
-- coding: utf-8 --
class FamilyNode:
-- coding: utf-8 --
class PersonInfo:
利用队列先进先出的特性
import json
import queue
from PersonInfo import PersonInfo
from FamilyNode import FamilyNode
with open("./家谱文件/1.json", encoding = "utf-8") as 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:
itemList中存放着新的数据结构