Key type of QJsonObject



  • 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": {}
    }
    

  • Lifetime Qt Champion

    Hi,

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



  • @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"
    

  • Moderators

    @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.



  • Thank you guys.

    What a wonderful quick responses.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.