where is QJsonObject
-
Hello,
where do I find
QJsonObject
in PySide6?from PySide6.QtCore import QJsonDocument, QJsonObject print(PySide6.__version__) a = QJsonDocument() b = QJsonObject()
6.8.0.2
ImportError: cannot import name 'QJsonObject' from 'PySide6.QtCore'
According to the documentation
QJsonObject
is located inQtCore
asQJsonDocument
,QJsonArray
,QJsonValue
. But it is not found.C++ is working:
#include <QCoreApplication> #include <QDebug> #include <QJsonObject> int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); auto a = new QJsonObject(); qDebug() << a; }
0x55e154300560
-
@MasterQ
Nope, in the sense of Qt XML C++ ClassesNote: Qt XML will no longer receive additional features. For reading or writing XML documents iteratively (SAX), use the QXmlStreamReader and QXmlStreamWriter classes. The classes are both easier to use and more compliant with the XML standard.
They are removing high-level support, you have to use low-level calls and roll your own higher level stuff.
XML can create complex "documents", but is heavier weight. IMHO JSON barely creates a "document", it just saves/restores arrays and objects of a few base types in a limited structure. Which is fine if that's all you want.
Again, I don't think Qt libraries themselves use either JSON or XML so it's really for your internal use and from Python you can use its own support for these if you want more than Qt offers.
-
@MasterQ
Answer in two parts.Documentation notwithstanding, I think you will find there is no such Python/PySide/PyQt type as
QJsonObject
. In Python this is just a dictionary of key-values,dict_keys
, and that is what will be returned and you should use. Similarly I imagineQJsonArray
will be a Python array.However, when I try to test it I get no further with the following code snippet:
from PySide6.QtCore import QCoreApplication, QJsonDocument app = QCoreApplication([]) a = QJsonDocument() doc = QJsonDocument.fromJson(b"{ 'key1': '0' }") print(doc, doc.isObject(), doc.isEmpty(), doc.toJson()) obj = doc.object() print(obj) print(obj.keys()) print(obj['key1'])
This shows
QJsonDocument.fromJson(b"{ 'key1': '0' }")
produces an empty document which, unless I am going mad, is not right, I believe that is correct JSON and should produce a document with an object with akey1
key, but it doesn't. Nor in PyQt6. So I am a bit lost. If anyone would care to show me what i am doing wrong here that would be great! [EDIT See post later on.]Since so far as I know Qt does not actually use its QJson classes for anything internally you may find that using Python's own support for JSON is just as useful/better than using the Qt classes (which i appear to have trouble getting working).
-
@MasterQ
Nope, in the sense of Qt XML C++ ClassesNote: Qt XML will no longer receive additional features. For reading or writing XML documents iteratively (SAX), use the QXmlStreamReader and QXmlStreamWriter classes. The classes are both easier to use and more compliant with the XML standard.
They are removing high-level support, you have to use low-level calls and roll your own higher level stuff.
XML can create complex "documents", but is heavier weight. IMHO JSON barely creates a "document", it just saves/restores arrays and objects of a few base types in a limited structure. Which is fine if that's all you want.
Again, I don't think Qt libraries themselves use either JSON or XML so it's really for your internal use and from Python you can use its own support for these if you want more than Qt offers.
-
-
@MasterQ
FWIW, thanks to @J-Hilk corrected my attempted example so it actually works, I forgot JSON insists on double-quotes not single-quotes:from PySide6.QtCore import QCoreApplication, QJsonDocument, QJsonParseError app = QCoreApplication([]) a = QJsonDocument() err = QJsonParseError() doc = QJsonDocument.fromJson(b'{ "key1": 0 }', err) print(doc, doc.isObject(), doc.isEmpty(), doc.toJson(), err.errorString()) obj = doc.object() print(obj) print(obj.keys()) print(obj['key1'])