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. Key type of QJsonObject
QtWS25 Last Chance

Key type of QJsonObject

Scheduled Pinned Locked Moved Solved General and Desktop
jsonqjsonobjectjson parser
5 Posts 3 Posters 5.3k 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.
  • tansgumusT Offline
    tansgumusT Offline
    tansgumus
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      tansgumusT 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

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

        tansgumusT Offline
        tansgumusT Offline
        tansgumus
        wrote on last edited by tansgumus
        #3

        @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"
        
        JKSHJ 1 Reply Last reply
        0
        • tansgumusT 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"
          
          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          @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
          3
          • tansgumusT Offline
            tansgumusT Offline
            tansgumus
            wrote on last edited by
            #5

            Thank you guys.

            What a wonderful quick responses.

            1 Reply Last reply
            1

            • Login

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