先贴上代码:
# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'json_converter.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication
import json
import sysclass Ui_Form(object):def __init__(self):super().__init__() def setupUi(self, Form):Form.setObjectName("JSON Viewer")Form.resize(1265, 725)self.label = QtWidgets.QLabel(Form)self.label.setObjectName("label")self.label.setGeometry(QtCore.QRect(10, 20, 72, 15))self.lineEdit = QtWidgets.QLineEdit(Form)self.lineEdit.setObjectName("lineEdit")self.lineEdit.setEnabled(True)self.lineEdit.setGeometry(QtCore.QRect(90, 20, 1061, 21))self.lineEdit.setReadOnly(True)self.toolButton = QtWidgets.QToolButton(Form)self.toolButton.setObjectName("toolButton")self.toolButton.setGeometry(QtCore.QRect(1160, 20, 47, 21))self.toolButton.clicked.connect(self.getJSONFile)self.tableView = QtWidgets.QTableView(Form)self.tableView.setGeometry(QtCore.QRect(10, 60, 1241, 611))self.tableView.setObjectName("tableView")self.tableView.horizontalHeader().setVisible(True)self.convertButton = QtWidgets.QPushButton(Form)self.convertButton.setObjectName("convertButton")self.convertButton.setGeometry(QtCore.QRect(10, 680, 93, 28)) self.convertButton.clicked.connect(self.loadJsonFile)self.pushButton = QtWidgets.QPushButton(Form)self.pushButton.setObjectName("pushButton")self.pushButton.setGeometry(QtCore.QRect(1160, 680, 93, 28)) self.pushButton.clicked.connect(Form.close)self.retranslateUi(Form)QtCore.QMetaObject.connectSlotsByName(Form)def retranslateUi(self, Form):_translate = QtCore.QCoreApplication.translateForm.setWindowTitle(_translate("JSON Viewer", "JSON Viewer"))self.pushButton.setText(_translate("Close", "Close"))self.label.setText(_translate("JSON File","JSON File"))self.convertButton.setText(_translate("Load JSON File","Load JSON File"))def setColumnHeader(self, columnHeader):self.model=QtGui.QStandardItemModel()self.model.setHorizontalHeaderLabels(columnHeader) self.tableView.horizontalHeader().setModel(self.model)def show(self, Form):Form.show()def close(self, Form):Form.closedef addTableLine(self,row):item=[]for i in range(len(row)):item.append(QtGui.QStandardItem(row[i]))self.model.appendRow(item) self.tableView.setModel(self.model)def getJSONFile(self):fname=QtWidgets.QFileDialog.getOpenFileName(caption="Open file", directory="",filter="Json files (*.json)") self.lineEdit.setText(fname[0])def loadJsonFile(self):fname=self.lineEdit.text()f=open(fname,encoding="utf-8")header_list=[]data=json.load(f)f.closefor i in data['data'][0]:header_list.append(i)self.setColumnHeader(header_list)for i in data['data']:item=[]for j in i:item.append(QtGui.QStandardItem(i[j]))self.addTableLine(item)if __name__ == "__main__":app = QApplication(sys.argv)Form=QtWidgets.QWidget() main_win = Ui_Form() main_win.setupUi(Form)main_win.show(Form)sys.exit(app.exec())
项目运行时的截图:
这个项目使用Qt Designer创建的。