Multiple JSON objects to one JSON object
Solved
General and Desktop
-
While parsing INI file, I got various QVariants which I converted to Json objects using the following code:
QSettings settings("test.ini",QSettings::IniFormat); QStringList groups= settings.childGroups(); foreach (QString group, groups) { qDebug() << group; settings.beginGroup(group); QStringList keys = settings.childKeys(); foreach (key, keys) { values=settings.value(key); qDebug() << values; QJsonObject object; QJsonValue valueJson(values.toString()); object.insert(key,valueJson); qDebug()<<object; } settings.endGroup();
Now I have multiple QVariants and QJsonObjects like this:
QVariant(QString, "value1") QJsonObject({"name1":"value1"}) QVariant(QString, "value2") QJsonObject({"name2":"value2"}) QVariant(QString, "value3") QJsonObject({"name3":"value3"})
How can I get one JsonObject that looks like this:
QJsonObject({"name1":"value1"},{"name2":"value2"},{"name3":"value3"})
-
Here is the latest I've tried:
QSettings settings("test.ini",QSettings::IniFormat); QStringList groups= settings.childGroups(); foreach (QString group, groups) { qDebug() << group; settings.beginGroup(group); QStringList keys = settings.childKeys(); foreach (key, keys) { values=settings.value(key); qDebug() << values; QJsonObject object; QJsonValue valueJson(values.toString()); object.insert(key,valueJson); qDebug()<<object; foreach (valueJson, object) { QJsonArray arr; arr.append(object); qDebug()<<arr; } } settings.endGroup(); }
-
Thank you for the suggestion. I've tried something like this:
QJsonArray arr; arr.append(object); qDebug()<<arr;
but whatever I do, I get QJsonArray with only one Json Object and the output looks like this:
QVariant(QString, "value1") QJsonObject({"name1":"value1"}) QJsonArray([{"name1":"value1"}]) QVariant(QString, "value2") QJsonObject({"name2":"value2"}) QJsonArray([{"name2":"value2"}]) QVariant(QString, "value3") QJsonObject({"name3":"value3"}) QJsonArray([{"name3":"value3"}])
I know I have to use for loop somehow, but I cant get it right.
-
Here is the latest I've tried:
QSettings settings("test.ini",QSettings::IniFormat); QStringList groups= settings.childGroups(); foreach (QString group, groups) { qDebug() << group; settings.beginGroup(group); QStringList keys = settings.childKeys(); foreach (key, keys) { values=settings.value(key); qDebug() << values; QJsonObject object; QJsonValue valueJson(values.toString()); object.insert(key,valueJson); qDebug()<<object; foreach (valueJson, object) { QJsonArray arr; arr.append(object); qDebug()<<arr; } } settings.endGroup(); }
-
the array goes outside:
QSettings settings("test.ini",QSettings::IniFormat); QStringList groups= settings.childGroups(); QJsonObject iniObj; foreach (QString group, groups) { settings.beginGroup(group); QStringList keys = settings.childKeys(); QJsonArray keys; foreach (key, keys) { values=settings.value(key); QJsonObject object; QJsonValue valueJson(values.toString()); object.insert(key,valueJson); keys.append(object); } iniObj.insert(group,keys); settings.endGroup(); }
-
Omg, that's right. Thank you so much!
-
@YouKnowMe Please leave the messages (don't delete them) after you find a solution. This allows other people to read your problem and benefit from the solution.
I have restored the deleted messages.