Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Empty QJsonDocument from file
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Empty QJsonDocument from file

Scheduled Pinned Locked Moved General and Desktop
28 Posts 4 Posters 17.1k Views 1 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.
  • N Offline
    N Offline
    ningu
    wrote on last edited by
    #4

    I think the error is in the loading of the file.
    @qDebug() << file.bytesAvailable();@

    outputs 0, although it seems to load fine (no error output).

    1 Reply Last reply
    0
    • N Offline
      N Offline
      ningu
      wrote on last edited by
      #5

      If I load the data from the file to QDomDocument object it seems to work fine. I very puzzled.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #6

        Hi,

        Just to be on the safe side, did you properly put your json file in the resources (qrc file) ?

        You keep talking about QDomDocument, is it a typo ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • N Offline
          N Offline
          ningu
          wrote on last edited by
          #7

          Yes, the file is properly put in my qrc file.

          And yes, sorry, when I said

          bq. Unfortunately, even though you are right, removing the superfluous comma did not solve the problem. The QDomDocument is still empty.

          I meant the QJsonDocument is still empty.

          But later on I tried to load the data into a QDomDocument object just to try and it seems to work fine.
          @
          QDomDocument *domDocument;
          if (!domDocument->setContent(&file)) {
          qDebug() << "Could not set DOM";
          return;
          }

          qDebug() << domDocument->toString();@

          prints the file contents to the console.

          Sorry for the confusion I created with the typo.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #8

            Ok, then what does

            @qDebug() << file.readAll()@

            return ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • N Offline
              N Offline
              ningu
              wrote on last edited by
              #9

              It returns
              @""@

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #10

                Then it seems that your file is empty

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  ningu
                  wrote on last edited by
                  #11

                  That's what I thought, but
                  @qDebug() << QString::fromUtf8(file.readAll())@
                  returns the file contents.

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    ningu
                    wrote on last edited by
                    #12

                    Might it be an encoding issue?

                    1 Reply Last reply
                    0
                    • N Offline
                      N Offline
                      ningu
                      wrote on last edited by
                      #13

                      Sorry, that was not completely right:
                      @QString jsonString = QString::fromUtf8(file.readAll());
                      qDebug() << jsonString;
                      @
                      returns the file contents, while
                      @qDebug() << QString::fromUtf8(file.readAll())@
                      returns
                      @""@

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #14

                        Strange, QJsonDocument should handle it...
                        Did you check what error you got when using fromJson ?

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • N Offline
                          N Offline
                          ningu
                          wrote on last edited by
                          #15

                          No, I didn't. How could I do that?

                          1 Reply Last reply
                          0
                          • N Offline
                            N Offline
                            ningu
                            wrote on last edited by
                            #16

                            Ok, I got it. A friend of mine pointed me out that I was looking for the array in the QJsonDocument, and that I should probably be looking for the object, which worked.

                            The only thing that doesn't work right is that
                            @QString jsonString = QString::fromUtf8(file.readAll());

                            QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toUtf8());@
                            works, but
                            @QJsonDocument jsonDoc = QJsonDocument::fromJson(file.readAll());@
                            doesn't.

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #17

                              Look at the second parameter of "fromJson":http://qt-project.org/doc/qt-5.0/qtcore/qjsondocument.html#fromJson

                              From there you can get the error

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • N Offline
                                N Offline
                                ningu
                                wrote on last edited by
                                #18

                                It's an 'Illegal value' error. But I get the same error even with an empty file.

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #19

                                  Could it be your json file encoding that is problematic ?

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  0
                                  • N Offline
                                    N Offline
                                    ningu
                                    wrote on last edited by
                                    #20

                                    That might be it. Is there a way to make sure the file is UTF8? Or set it to UTF8?

                                    1 Reply Last reply
                                    0
                                    • SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #21

                                      If you are using QtCreator you could check the editor settings to ensure that it uses UTF8

                                      Interested in AI ? www.idiap.ch
                                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      0
                                      • N Offline
                                        N Offline
                                        ningu
                                        wrote on last edited by
                                        #22

                                        Ah, that did it!

                                        Thank you very much for all your help SGaist!

                                        1 Reply Last reply
                                        0
                                        • SGaistS Offline
                                          SGaistS Offline
                                          SGaist
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #23

                                          You're welcome !

                                          If everything is working now, please update the thread's title to solved so other forum users may know a solution has been found :)

                                          Interested in AI ? www.idiap.ch
                                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          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