Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Getting multiple values from one key



  • If I have INI file with this format:

    [TAG1]
    name=value1,value2,value3,...
    
    [TAG2]
    name=value1,value2,value3,...
    
    [TAG3]
    name=value1,value2,value3,...
    

    How to get JSON object that looks like this:

    {"TAG1":["value1","value2",...], "TAG2":["value1","value2",...]}
    

    I've tried to get them like this:

    QSettings settings(":/data.ini",QSettings::IniFormat);
    QStringList groups= settings.childGroups();
    foreach (QString group, groups)
    {
        settings.beginGroup(group);
        QStringList keys = settings.childKeys();
        QJsonArray arr;
        foreach (key, keys) {
            values=settings.value(key);
            QJsonObject object;
            QJsonValue valueJson(values.toString());
            object.insert(key,valueJson);
            arr.append(object);
        }
        qDebug() <<"Array: "<< arr;
        jsonObj.insert(group,arr);
        qDebug()<<jsonObj;
    
        settings.endGroup()
    
        }
    

    But what I get as QJsonObject is:

    QJsonObject({"TAG1":[{"name":""}],"TAG2":[{"name":""}],"TAG3":[{"name":""}]})
    I dont want "name", only values as I wrote up there.


  • Lifetime Qt Champion

    @Guest said in Getting multiple values from one key:

    settings.value(key);

    Since the returned QVariant holds a QStringList QVariant::toString() will return an empty string since there is no automatic conversion between a QStringList and a QString


Log in to reply