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
Qt 6.11 is out! See what's new in the release blog

[SOLVED] Empty QJsonDocument from file

Scheduled Pinned Locked Moved General and Desktop
28 Posts 4 Posters 18.0k 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
    #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
                          • V Offline
                            V Offline
                            VTiTux
                            wrote on last edited by
                            #24

                            Hi,

                            I have the same problem, but the solution isn't the UTF8 coding, for me.
                            QtCreator is already configured to UTF, and that
                            @ QString jsonString = QString::fromUtf8(file.readAll());

                            QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toUtf8());
                            

                            @
                            don't work for me.

                            My JSON file is built and written by JsonDocument and QFile, so, it could be valide:
                            @
                            bool JSONStorage::storeJSONFile(){
                            if( !jsonFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate) ){
                            return false;
                            }
                            QJsonDocument jsonDocument( jsonArray );
                            jsonFile.write( jsonDocument.toJson() );
                            jsonFile.close();
                            return true;
                            }
                            @

                            But I do not manage to load this file. I use this method:
                            @
                            void JSONStorage::openJSONFile(){
                            QJsonParseError parseErr;
                            QJsonDocument jsonDocument;

                            if( !jsonFile.open(QIODevice::ReadOnly | QIODevice::Text) ){
                                return;
                            }
                            
                            jsonDocument.fromJson( jsonFile.readAll(), &parseErr );
                            qDebug() << parseErr.errorString();
                            if( jsonDocument.isEmpty()){
                                qDebug() << "Empty!";
                            }
                            if( jsonDocument.isArray() ){
                                qDebug() << "Is array";
                                jsonArray = jsonDocument.array();
                            }
                            

                            }
                            @

                            It says that is empty, and parseErr: "no error occurred"

                            If I write:
                            @
                            qDebug() << jsonFile.readAll();
                            @

                            I can see my document. It isn't empty!

                            What is wrong?

                            1 Reply Last reply
                            0
                            • V Offline
                              V Offline
                              VTiTux
                              wrote on last edited by
                              #25

                              Is it because my main object is an array?

                              I go to eating and I will retry with an JsonObject to the root, later.

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

                                Use the second parameter of fromJson(const QByteArray & json, QJsonParseError * error = 0) and check what it returns

                                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
                                • V Offline
                                  V Offline
                                  VTiTux
                                  wrote on last edited by
                                  #27

                                  Ok

                                  It isn't that:
                                  @
                                  jsonDocument.fromJson( jsonFile.readAll(), &parseErr );
                                  @
                                  but that:
                                  @
                                  jsonDocument = QJsonDocument::fromJson(jsonFile.readAll(), &parseErr );
                                  @

                                  Thanks.

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

                                    Indeed, fromJson is a static function

                                    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