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 not loading fromJson
Forum Updated to NodeBB v4.3 + New Features

[solved] QJsonDocument not loading fromJson

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 3.3k 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.
  • D Offline
    D Offline
    davidgildea
    wrote on last edited by
    #1

    Hi,

    I have read the other posts on here regarding problems with Qt5 and QJsonDocument but I am having a problems with it and none of the posts seem to help. Its not loading any content form the fromJson static method. No errors are reported QParseError.

    Here is the code I have:
    @
    QFile file;
    file.setFileName("c:\jsondata\data.txt");
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    QString val = QString::fromUtf8(file.readAll());

    QJsonParseError parseErr;

    QJsonDocument jsonDoc = QJsonDocument::fromJson(val.toUtf8(),&parseErr);
    file.close();

    QJsonObject jsonObject = jsonDoc.object();
    QJsonArray jsonArray = jsonObject["server"].toArray();
    @

    But jsonDoc is always null.

    Any ideas why that might be, Hope someone can help me and I havent done something really stupid.

    Many Thanks

    Dave

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

      Hi and welcome to devnet,

      What does parseErr return ?

      Also why store in a QString to revert back to a QByteArray ?

      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
      • D Offline
        D Offline
        davidgildea
        wrote on last edited by
        #3

        Many thanks for getting back so fast.

        &ParseErr has a value of "no error".

        I have purposely added in characters into the text to produce and error and it does catch it when the json in the file is invalid, so I know that its getting that far and able to try and parse the json.

        As for converting from QByteArry to QString and back that was just to check the text from the file was successfully being parsed. I have tried using the file.readAll() in fromJson and it still does not work..

        Thanks

        Dave

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

          What does data.txt contain ?

          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
          • D Offline
            D Offline
            davidgildea
            wrote on last edited by
            #5

            I copied if from one of the previous QJsonDocument questions posted here as it worked form that poster:

            {
            "osc_port": 57120,
            "server": {
            "path": "/path/to/executable",
            "ip": "127.0.0.1",
            "port": 54321
            },
            "data_path": "/path/to/data/folder",
            "frame_rate": 25,
            "vertical_sync": true,
            "threshold": 64,
            "camera": {
            "device_id": 0,
            "width": 640,
            "height": 480,
            "rotate_180": false,
            "use_uvc": false
            },
            "background_color": {
            "red": 64,
            "green": 64,
            "blue": 64,
            "alpha": 255
            },
            "tool_highlight_color": {
            "red": 240,
            "green": 64,
            "blue": 64,
            "alpha": 128
            }
            }

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

              Then I see your problem:

              @QJsonArray jsonArray = jsonObject["server"].toArray();@

              jsonObject["server"] does not return an array

              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
              • D Offline
                D Offline
                davidgildea
                wrote on last edited by
                #7

                Your a legend, I had tried different objects earlier and they had not worked but I must had another problem that was causing me trouble.

                This works for me now:
                @
                QJsonObject jsonObject = jsonDoc.object();
                qDebug() << jsonObject.value(QString("osc_port"));
                @

                Again, many thanks for pointing out the error of my ways.

                Is there a way for me to close this question as solved or award you a +1 for helping resolved my issue ?
                Dave

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

                  You're welcome !

                  The only way to do that is to edit your thread title prepending [solved] (not a real closing but it gives hint to other users that a solution has been found)

                  There's currently no answer ranking system (could be useful though)

                  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