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] QJsonDocument parsing issue Qt 5.3 vs. Qt 5.4 on OSX using C++11

[Solved] QJsonDocument parsing issue Qt 5.3 vs. Qt 5.4 on OSX using C++11

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 2.9k Views
  • 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.
  • M Offline
    M Offline
    Manromen
    wrote on 19 Jan 2015, 08:46 last edited by
    #1

    Hi there,

    I have problems parsing json since I upgraded to Qt 5.4.

    Here is an example:
    @#include <QCoreApplication>

    #include <QJsonDocument>
    #include <QJsonObject>
    #include <QJsonArray>

    #include <QDebug>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    char jsString[] {
        "{\"results\":[{\"id\":1,\"title\":\"Test1\"},{\"id\":2,\"title\":\""
        "Test2\"},{\"id\":3,\"title\":\"Test3\"},{\"id\":4,\"title\":\"Test4\"}]}"
    };
    
    QJsonParseError *error { nullptr };
    // parse bytes to json
    QJsonDocument doc { QJsonDocument::fromJson(jsString, error) };
    
    if (error) {
        qDebug() << "error parsing json:" << error->errorString();
    } else {
    
        QJsonObject rootObj { doc.object() };
        QJsonArray results { rootObj.value("results").toArray() };
    
        qDebug() << "results.count:" << results.count();
    
        for (QJsonValue v : results) {
            qDebug() << "v:" << v.toObject().value("title").toString();
        }
    }
    return a.exec(&#41;;
    

    }@

    If I run this using Qt 5.3 all is fine. The output is:
    @results.count: 4
    v: "Test1"
    v: "Test2"
    v: "Test3"
    v: "Test4"@

    If I run this using Qt 5.4 I get this:
    @results.count: 1
    v: ""@

    I run this on Mac OS X Yosemite 64-Bit with the clang compiler.

    Has anyone an idea whats wrong?

    Cheers,
    Manromen

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 20 Jan 2015, 20:34 last edited by
      #2

      Hi and welcome to devnet,

      Can't really comment on this, the only thing I can say is that it works fine when not using C++11

      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
      • M Offline
        M Offline
        Manromen
        wrote on 22 Jan 2015, 13:34 last edited by
        #3

        Hi,

        thank you for your Response.

        I replaced
        @QJsonArray results { rootObj.value("results").toArray() };@
        with:
        @QJsonArray results = rootObj.value("results").toArray();@

        So now it works ....

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 22 Jan 2015, 23:31 last edited by
          #4

          Sill, it's a bit strange that it worked with Qt 5.3 and not 5.4

          Did you change anything else in between ? (e.g. compiler)

          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
          • M Offline
            M Offline
            Manromen
            wrote on 22 Jan 2015, 23:47 last edited by
            #5

            I just switched between the Kits:
            Desktop Qt 5.3 clang 64bit
            and
            Desktop Qt 5.4.0 clang 64bit

            Both Kits use the Clang (x86 64bit in /usr/bin).
            Only difference I can see is that the Qt 5.3 Kit has no Debugger.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 23 Jan 2015, 00:23 last edited by
              #6

              Then I'd recommend checking the "bug report system":http://bugreports.qt.io to see if it's something known. If not please consider opening a new report providing a minimal compilable example to reproduce the behavior

              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
              • J Offline
                J Offline
                JKSH
                Moderators
                wrote on 23 Jan 2015, 02:37 last edited by
                #7

                The Qt 5.4 behaviour is the correct, "documented":http://doc.qt.io/qt-5/qjsonarray.html#QJsonArray-2 one.

                rootObj.value("results").toArray() returns a QJsonArray containing 4 elements. However, by using the initializer-list, this array is first converted into a single QJsonValue. Then, this single QJsonValue is stored in the outer array. That's why the outer array reports having 1 element.

                Do this and you will see that you have a 2D array (more precisely, it's an array-within-an-array):
                @
                QJsonArray results{ rootObj.value("results").toArray() };
                qDebug() << results;
                @

                Do this and you will see that you have a 1D array:
                @
                QJsonArray results = rootObj.value("results").toArray();
                qDebug() << results;
                @

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Manromen
                  wrote on 23 Jan 2015, 08:06 last edited by
                  #8

                  Hi JKSH,

                  makes absolutely sense. Thank you very much for this great explanation!

                  Cheers,
                  Manromen

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    JKSH
                    Moderators
                    wrote on 23 Jan 2015, 08:55 last edited by
                    #9

                    You're welcome :)

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 23 Jan 2015, 22:33 last edited by
                      #10

                      Okay… Just saw that my offline doc was from the wrong Qt version :D
                      Thanks for the explanation JKSH

                      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

                      1/10

                      19 Jan 2015, 08:46

                      • Login

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