Foreach Through QJsonObject to get Key/Value Pair

后端 未结 1 1095
刺人心
刺人心 2021-01-13 12:00

I am wondering how I would foreach through a QJsonObject to get the key/value pairs in C++? So far, I am only able to get the value.



        
1条回答
  •  攒了一身酷
    2021-01-13 12:54

    John already gave the answer. Using keys() a complete working solution would be:

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        //main.cpp
        QFile file("path/to/geoip.json");
        file.open(QIODevice::ReadOnly);
        QByteArray rawData = file.readAll();
        file.close();
        QJsonDocument doc(QJsonDocument::fromJson(rawData));
        QJsonObject json = doc.object();
        foreach(const QString& key, json.keys()) {
            QJsonValue value = json.value(key);
            qDebug() << "Key = " << key << ", Value = " << value.toString();
        }
    
        return a.exec();
    }
    

    0 讨论(0)
提交回复
热议问题