Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. where is QJsonObject
Forum Updated to NodeBB v4.3 + New Features

where is QJsonObject

Scheduled Pinned Locked Moved Solved Qt for Python
8 Posts 3 Posters 1.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • MasterQM Offline
    MasterQM Offline
    MasterQ
    wrote on last edited by
    #1

    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 in QtCore as QJsonDocument, 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
    
    JonBJ Pl45m4P 2 Replies Last reply
    0
    • MasterQM MasterQ

      Is the Qt support for XML better?

      I haven't made up my mind yet and maybe XML could be a better choice, even if Pythons native JSON support is very handy.

      XML, in general, seems much more complex as JSON in creating "documents".

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #4

      @MasterQ
      Nope, in the sense of Qt XML C++ Classes

      Note: 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.

      1 Reply Last reply
      0
      • MasterQM MasterQ

        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 in QtCore as QJsonDocument, 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
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #2

        @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 imagine QJsonArray 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 a key1 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).

        1 Reply Last reply
        0
        • MasterQM Offline
          MasterQM Offline
          MasterQ
          wrote on last edited by MasterQ
          #3

          Is the Qt support for XML better?

          I haven't made up my mind yet and maybe XML could be a better choice, even if Pythons native JSON support is very handy.

          XML, in general, seems much more complex as JSON in creating "documents".

          JonBJ 1 Reply Last reply
          0
          • MasterQM MasterQ

            Is the Qt support for XML better?

            I haven't made up my mind yet and maybe XML could be a better choice, even if Pythons native JSON support is very handy.

            XML, in general, seems much more complex as JSON in creating "documents".

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #4

            @MasterQ
            Nope, in the sense of Qt XML C++ Classes

            Note: 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.

            1 Reply Last reply
            0
            • MasterQM MasterQ has marked this topic as solved on
            • JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #5

              @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'])
              
              1 Reply Last reply
              1
              • MasterQM MasterQ

                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 in QtCore as QJsonDocument, 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
                
                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by
                #6

                @MasterQ said in where is QJsonObject:

                C++ is working

                Of course it is ;-)

                Jokes aside, if you don't want to use the "native" Python types as suggested by @JonB, why not use C++ directly?


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                1 Reply Last reply
                0
                • MasterQM Offline
                  MasterQM Offline
                  MasterQ
                  wrote on last edited by
                  #7

                  native Python types are fully OK. I just wanted to test, what is better, native Python or Qt.

                  1 Reply Last reply
                  0
                  • JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #8

                    Like I said, Qt's support for JSON or XML is not as comprehensive as Python's, and you can afford to use the latter in a Qt Python program if you want better support.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved