Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Key type of QJsonObject

    General and Desktop
    json qjsonobject json parser
    3
    5
    4486
    Loading More Posts
    • 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.
    • tansgumus
      tansgumus last edited by

      Hi,

      This is my fist time of using json under Qt.

      I want to read any json file just like this js app but I don't know how to iterate QJsonObject correctly and how can I check the type of QJsonObject (object, element or array)

      QJsonObject MainWindow::parser(QString fileName)
      {
          QString val;
          QFile file;
          file.setFileName(fileName);
          file.open(QIODevice::ReadOnly | QIODevice::Text);
          val = file.readAll();
          file.close();
      
          QJsonDocument document = QJsonDocument::fromJson(val.toUtf8());
          QJsonObject object = document.object();
      
          QJsonObject::iterator i;
          for (i = object.begin(); i != object.end(); ++i) {
              if (i.value().isNull())
                  qDebug() << i.key();
              else
                  qDebug() << i.key() << i.value();
          }
      }
      

      The debuggers show whole json file content!

      "FOOD PLAN" QJsonValue(object, QJsonObject({}))
      "WORKOUT PLAN" QJsonValue(object, QJsonObject({"GYM":{},"HOME":{"DAY1":{},"DAY2":{"Exercise 1":{"Name":"name_vale","Narrator":"http://","Sets":{"12":"30 sec","3":"10 sec","5":"3 sec"},"Tip":"some tips","URL":"http://"},"Exercise 2":{},"Tip":"some tip"},"Tip":"some tip"}}))
      

      A demo json for test:

      {
      	"WORKOUT PLAN": {
      		"GYM": {},
      		"HOME": {
      			"DAY1": {},
      			"DAY2": {
      				"Exercise 1": {
      					"Name": "name_vale",
      					"URL": "http://",
      					"Tip": "some tips",
      					"Narrator": "http://",
      					"Sets": {
      						"3": "10 sec",
      						"5": "3 sec",
      						"12": "30 sec"
      					}
      				},
      				"Exercise 2": {},
      				"Tip": "some tip"
      			},
      			"Tip": "some tip"
      		}
      	},
      	"FOOD PLAN": {}
      }
      
      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        QJsonValue provides several isXXX method to know what you are dealing with.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        tansgumus 1 Reply Last reply Reply Quote 1
        • tansgumus
          tansgumus @SGaist last edited by tansgumus

          @SGaist said in Key type of QJsonObject:

          Hi,

          QJsonValue provides several isXXX method to know what you are dealing with.

          Thanks a lot. But my code doesn't show the whole content of json file! It just shows the first objects!

          QJsonObject MainWindow::parser(QString fileName)
          {
              QString val;
              QFile file;
              file.setFileName(fileName);
              file.open(QIODevice::ReadOnly | QIODevice::Text);
              val = file.readAll();
              file.close();
          
              QJsonDocument document = QJsonDocument::fromJson(val.toUtf8());
              QJsonObject object = document.object();
          
              QJsonObject::iterator i;
              for (i = object.begin(); i != object.end(); ++i) {
                  if (i.value().isObject()) {
                      qDebug() << "OBJECT" << i.key();
                  } else if (i.value().isArray()) {
                      QString array_data;
                      foreach (QVariant value, i.value().toArray().toVariantList()) {
                          array_data += value.toString() + ", ";
                      }
                      qDebug() << "ARRAY" << array_data;
                  } else {
                      qDebug() << i.key() << i.value().toString();
                  }
          
              }
          }
          

          What's wrong?!

          log

          OBJECT "FOOD PLAN"
          OBJECT "WORKOUT PLAN"
          
          JKSH 1 Reply Last reply Reply Quote 0
          • JKSH
            JKSH Moderators @tansgumus last edited by

            @tansgumus said in Key type of QJsonObject:

            my code doesn't show the whole content of json file! It just shows the first objects!

            An iterator only searches one "level". Your top-level object only has 2 members: "WORKOUT PLAN" and "FOOD PLAN".

            You need to create a new QJsonObject::iterator for each "level". Something like this:

            if (i.value().isObject()) {
                qDebug() << "OBJECT" << i.key();
                QJsonObject innerObject = i.value().toObject();
                QJsonObject::iterator j;
                for (j = innerObject.begin(); j != innerObject.end(); ++j) {
                    if (j.value().isObject()) {
                        qDebug() << '\t' << "OBJECT" << j.key();
                        // ...
                    }
                    // ...
                }
            }
            

            Now, your output will be:

            OBJECT "FOOD PLAN"
            OBJECT "WORKOUT PLAN"
                OBJECT "GYM"
                OBJECT "HOME"
            

            Of course, since your JSON document object has 6 levels, you should write recursive functions to process your whole document. Otherwise, your code will get too messy.

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

            1 Reply Last reply Reply Quote 3
            • tansgumus
              tansgumus last edited by

              Thank you guys.

              What a wonderful quick responses.

              1 Reply Last reply Reply Quote 1
              • First post
                Last post