杰克逊如何将JsonNode转换为ArrayNode而不转换?(Jackson how to transform JsonNode to ArrayNode without casting?)
我正在将JSON库从org.json更改为Jackson,我想迁移以下代码:
JSONObject datasets = readJSON(new URL(DATASETS)); JSONArray datasetArray = datasets.getJSONArray("datasets");
现在在杰克逊我有以下:
ObjectMapper m = new ObjectMapper(); JsonNode datasets = m.readTree(new URL(DATASETS)); ArrayNode datasetArray = (ArrayNode)datasets.get("datasets");
但是我不喜欢那个演员,是否有
ClassCastException
的可能性? 有没有方法等同于getJSONArray
中的org.json
以便我有适当的错误处理,以防它不是数组?I am changing my JSON library from org.json to Jackson and I want to migrate the following code:
JSONObject datasets = readJSON(new URL(DATASETS)); JSONArray datasetArray = datasets.getJSONArray("datasets");
Now in Jackson I have the following:
ObjectMapper m = new ObjectMapper(); JsonNode datasets = m.readTree(new URL(DATASETS)); ArrayNode datasetArray = (ArrayNode)datasets.get("datasets");
However I don't like the cast there, is there the possibility for a
ClassCastException
? Is there a method equivalent togetJSONArray
inorg.json
so that I have proper error handling in case it isn't an array?
原文:https://stackoverflow.com/questions/16788213
最满意答案
如果你想建立一个
QRadioButton
并且它是一个组中的独占,一个可能的解决方案是实现一个委托,在这个委托中,我们将覆盖paint
方法来绘制QRadioButton
和editorEvent
方法来捕获click事件并更改状态的其他项目根据情况。from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * import sys class Delegate(QStyledItemDelegate): def paint(self, painter, option, index): if not index.parent().isValid(): QStyledItemDelegate.paint(self, painter, option, index) else: widget = option.widget style = widget.style() if widget else QApplication.style() opt = QStyleOptionButton() opt.rect = option.rect opt.text = index.data() opt.state |= QStyle.State_On if index.data(Qt.CheckStateRole) else QStyle.State_Off style.drawControl(QStyle.CE_RadioButton, opt, painter, widget) def editorEvent(self, event, model, option, index): value = QStyledItemDelegate.editorEvent(self, event, model, option, index) if value: if event.type() == QEvent.MouseButtonRelease: if index.data(Qt.CheckStateRole) == Qt.Checked: parent = index.parent() for i in range(model.rowCount(parent)): if i != index.row(): ix = parent.child(i, 0) model.setData(ix, Qt.Unchecked, Qt.CheckStateRole) return value def main(): app = QApplication(sys.argv) tree = QTreeWidget() tree.setItemDelegate(Delegate()) for i in range(3): parent = QTreeWidgetItem(tree) parent.setText(0, "Parent {}".format(i)) for x in range(5): child = QTreeWidgetItem(parent) child.setFlags(child.flags() | Qt.ItemIsUserCheckable) child.setText(0, "Child {}".format(x)) child.setCheckState(0, Qt.Unchecked) tree.expandAll() tree.show() sys.exit(app.exec_()) if __name__ == '__main__': main()
If you want to establish a
QRadioButton
and it is exclusive in a group, a possible solution is to implement a delegate, in this delegate we will overwrite thepaint
method to draw theQRadioButton
and theeditorEvent
method to catch the click event and change the status of the other items according to be the case.from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * import sys class Delegate(QStyledItemDelegate): def paint(self, painter, option, index): if not index.parent().isValid(): QStyledItemDelegate.paint(self, painter, option, index) else: widget = option.widget style = widget.style() if widget else QApplication.style() opt = QStyleOptionButton() opt.rect = option.rect opt.text = index.data() opt.state |= QStyle.State_On if index.data(Qt.CheckStateRole) else QStyle.State_Off style.drawControl(QStyle.CE_RadioButton, opt, painter, widget) def editorEvent(self, event, model, option, index): value = QStyledItemDelegate.editorEvent(self, event, model, option, index) if value: if event.type() == QEvent.MouseButtonRelease: if index.data(Qt.CheckStateRole) == Qt.Checked: parent = index.parent() for i in range(model.rowCount(parent)): if i != index.row(): ix = parent.child(i, 0) model.setData(ix, Qt.Unchecked, Qt.CheckStateRole) return value def main(): app = QApplication(sys.argv) tree = QTreeWidget() tree.setItemDelegate(Delegate()) for i in range(3): parent = QTreeWidgetItem(tree) parent.setText(0, "Parent {}".format(i)) for x in range(5): child = QTreeWidgetItem(parent) child.setFlags(child.flags() | Qt.ItemIsUserCheckable) child.setText(0, "Child {}".format(x)) child.setCheckState(0, Qt.Unchecked) tree.expandAll() tree.show() sys.exit(app.exec_()) if __name__ == '__main__': main()
相关问答
更多-
PyQt [QTreeWidget]:如何为项目添加Radiobutton?(PyQt [QTreeWidget]: How to add Radiobutton for items?)[2022-02-21]
如果你想建立一个QRadioButton并且它是一个组中的独占,一个可能的解决方案是实现一个委托,在这个委托中,我们将覆盖paint方法来绘制QRadioButton和editorEvent方法来捕获click事件并更改状态的其他项目根据情况。 from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * import sys class Delegate(QStyledItemDelegate): ... -
http://doc.qt.io/qt-5/qtreewidget.html#setItemWidget将为您完成这项工作 只需使用您需要的任何小部件为每一行调用它。 干杯 /编辑: 假设您的列'status'具有列索引5 ,那么它将是这样的: QTreeWidgetItem* item = new QTreeWidgetItem(columnTextsStringList); /*insert into tree here*/ QProgressBar* progBar = new QProgressBa ...
-
PyQt5:如何将提示文本/图片设置为QTreeWidget中的项目?(PyQt5: How to set the hint text/picture to items in QTreeWidget?)[2023-12-11]
你可以使用html格式的函数setToolTip。 试试像这样: self.t_B1.setToolTip(0, 'Long long long text: show hint text, show pic B1
' % filename) 为每个QTreeWidgetItems。 它应该执行你想要的。 希望这个帮助! You can use the function setToolTip with html formating. Try something ... -
通过覆盖startDrag很容易实现: class PageTree(QTreeWidget): ... def startDrag(self, actions): if QtWidgets.qApp.keyboardModifiers() & Qt.MetaModifier: super().startDrag(actions) 请注意,我在这里使用了Meta(Windows)键,因为Ctrl和Shift修饰符与鼠标组合会影响选择的各个方面。 ( ...
-
老实说,我不太明白你的代码。 但是如果你想向QTreeWidget添加新项目,请按以下步骤操作: item = QTreeWidgetItem() item.setText(0, "John") # first name item.setText(1, "Doe") # last name item.setText(2, "35") # age self.treeWidget.addTopLevelItem(item) Honestly, I don't quite get your code. But ...
-
为避免在使用itemChanged信号时出现递归问题,请尝试暂时阻止信号,直到处理程序完成: def handle(self, item, column): self.treeWidget.blockSignals(True) if item.checkState(column) == QtCore.Qt.Checked: self.handleChecked(item, column) elif item.checkState(column) == QtCore.Q ...
-
您可以使用QTreeWidget.expandToDepth() 。 在你的情况下: from PyQt4 import QtCore, QtGui app = QtGui.QApplication([]) class Tree(QtGui.QTreeWidget): def __init__(self, *args, **kwargs): super(Tree, self).__init__() parentItem=QtGui.QTreeWidgetItem( ...
-
有一个expandAll()方法, QTreeWidget继承自QTreeView类。 因此,在调用loadUi() ,只需添加如下内容: self.treewWidget.expandAll() There is an expandAll() method, which QTreeWidget inherits from the QTreeView class. So, after calling loadUi(), just add something like this: self.tree ...
-
PyQT QTreeWidget迭代(PyQT QTreeWidget iterating)[2022-03-21]
您可以调用QTreeWidget.invisibleRootItem()来接收根项目,然后使用QTreeWidgetItem API遍历项目。 例: root = self.treeWidget.invisibleRootItem() child_count = root.childCount() for i in range(child_count): item = root.child(i) url = item.text(0) # text at first (0) column ... -
我假设您的问题的核心是您想要迭代树小部件并构建包含已检查项的字典。 下面的示例假设树是两级深度,并且不会尝试任何检查状态的管理: # not needed for python 3 import sip sip.setapi('QString', 2) from collections import defaultdict from PyQt4 import QtGui, QtCore class Window(QtGui.QWidget): def __init__(self): ...