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] QJsonDocumentt can't read with fromJson()
Qt 6.11 is out! See what's new in the release blog

[Solved] QJsonDocumentt can't read with fromJson()

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.7k 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.
  • U Offline
    U Offline
    uranusjr
    wrote on last edited by
    #1

    Hi,

    I'm trying to convert some existing JSON parsing code from QJson to Qt 5's JSON classes. The JSON files were parsed correctly with QJson, but can't be loaded into QJsonDocument. I used the following code:

    @
    QFile jsonFIle(":/path/to/json/file");
    jsonFile.open(QIODevice::ReadOnly | QIODevice::Text);
    QJsonDocument doc;
    QJsonParseError parseError;
    doc.fromJson(jsonFile.readAll(), &parseError);
    jsonFile.close();
    if (parseError.error != QJsonParseError::NoError)
    {
    // Report fatal error
    }
    // Analyze the content...
    @

    With the debugger I can see the file gets read, the encoding if certainly UTF-8 (I even tried QString(jsonFile.readAll()).toUtf8() just to make sure), and the content is correct. But I end up getting an empty QJsonDocument (doc.isNull() returns true). QJsonParseError offers no help at all, claiming the parsing ended with QJsonParseError::NoError (!?).

    Am I doing something wrong? The JSON files are certainly decodable (with Qt in UTF-8), and can be parsed correctly (I tested with QJson and Python's json.tool). And I can't understand why QJsonParseError keeps telling me there's no errors...

    Thanks in advance.

    Note: I'm using Qt 5.1 from the official installer (MSVC 2012 64-bit with OpenGL).

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

      Hi,

      You're using QJsonDocument wrong: fromJson is a static function.

      @QJsonDocument doc = QJsonDocument::fromJson(jsonFile.readAll(), &parseError);@

      and you should be good

      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
      1
      • U Offline
        U Offline
        uranusjr
        wrote on last edited by
        #3

        Obviously I got too much ahead of myself...Should have read the documentation more carefully. Thanks.

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

          You're welcome ! Happy coding !

          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