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.
  • 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