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, QJsonObject, are showing NULL and are not loading data correctly
QtWS25 Last Chance

[SOLVED]QJsonDocument, QJsonObject, are showing NULL and are not loading data correctly

Scheduled Pinned Locked Moved General and Desktop
15 Posts 2 Posters 9.6k 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.
  • D Offline
    D Offline
    doozii
    wrote on last edited by
    #3

    @/* test.json */
    {
    "appDesc": {
    "description": "SomeDescription",
    "message": "SomeMessage"
    },
    "appName": {
    "description": "Home",
    "message": "Welcome",
    "imp":["awesome","best","good"]
    }
    }

    void readJson()
    {
    QString val;
    QFile file;
    file.setFileName("test.json");
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    val = file.readAll();
    file.close();
    qWarning() << val;
    QJsonDocument d = QJsonDocument::fromJson(val.toUtf8());
    QJsonObject sett2 = d.object();
    QJsonValue value = sett2.value(QString("appName"));
    qWarning() << value;
    QJsonObject item = value.toObject();
    qWarning() << tr("QJsonObject of description: ") << item;

      /* incase of string value get value and convert into string*/
      qWarning() << tr("QJsonObject[appName] of description: ") << item["description"];
      QJsonValue subobj = item["description"];
      qWarning() << subobj.toString();
    
      /* incase of array get array and convert into string*/
      qWarning() << tr("QJsonObject[appName] of value: ") << item["imp"];
      QJsonArray test = item["imp"].toArray();
      qWarning() << test[1].toString();
    

    }@

    1 Reply Last reply
    0
    • D Offline
      D Offline
      doozii
      wrote on last edited by
      #4

      That is the example Im using but there are no values getting set in wither the QJsonDocument, the QJsonObject called sett2 or any combination of QJsonValue objects

      1 Reply Last reply
      0
      • D Offline
        D Offline
        doozii
        wrote on last edited by
        #5

        And I have the json in its own file called test.json that is being opened just as in the example. I created the json file by using the Add New > General > Text File and giving it a .json file extension.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          doozii
          wrote on last edited by
          #6

          And the abve is the ONLY decent example Ive been able to find as I scour Google and other searches... Is there any way for the QT Team to create some examples? Examples for other basics would be great too for those of us who are completely new to QT.

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

            You can ask for a documentation update on "the bug report system":http://bugreports.qt-project.org using a feature request.

            Just to be sure, did you got that example working properly ?

            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
              doozii
              wrote on last edited by
              #8

              no still not working... unable to find a working example and so far no clue as to what I'm doing wrong. Thanks for the heads up on the bug system. Any chance you can tell em whats wrong with the code example? Or provide an example that works? Ste-by-step would be nice. Thanks

              1 Reply Last reply
              0
              • D Offline
                D Offline
                doozii
                wrote on last edited by
                #9

                I ran just this part:
                @ file.setFileName("test.json");
                file.open(QIODevice::ReadOnly | QIODevice::Text);
                val = file.readAll();
                file.close();
                qWarning() << val;
                QJsonDocument d = QJsonDocument::fromJson(val.toUtf8());
                qWarning << d.isEmpty();
                @
                and received true.. in other words the document is not getting populated from the json. Is this a known issue?

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  doozii
                  wrote on last edited by
                  #10

                  Ok, I figured it out. Apparently, indenting your JSON file causes problems and the QJsonDocument can not populate. I removed all the spaces and test.json formatting for readability and its now populating. Thanks all

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

                    AFAIK, it should not be a problem. I've run the example and it's working fine. However what may happen is that your application can't find the file. You don't check that it's been opened successfully. Using a relative path, you must ensure that the file is besides your application or just give the absolute path to your json file

                    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
                      doozii
                      wrote on last edited by
                      #12

                      Nah, it was finding the file just fine and was reading into my QString just fine, but apparently it wasnt liking spaces or newlines or something cause it sure enough wasnt populating. But its great now. Doing exactly what I want. Thanks again

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

                        You might have had some stray non printable character in the JSON.

                        Anyway, glad it's working now.

                        Can you please update the thread title prepending [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
                        • D Offline
                          D Offline
                          doozii
                          wrote on last edited by
                          #14

                          I dont see a way to resolve this as solved

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

                            Just edit your first post and modify the title

                            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