This can be done by obtaining all the keys and then you can use the first method on the returned QStringList.
See the documentation for details.
Therefore, you would be writing something like this:
QJsonValue value = sett2.value(sett2.keys().first()); // NO Problem !!!
You could likely use the begin iterator as well as follows:
QJsonValue value = sett2.begin().value();
Here is the full sample project for reference:
main.json
{
"123": {
"from": "magellan.dunord@antartica-base.com",
"to": [
"lol@hotmail.fr",
"pipo@antartica-base.com"
],
"cc": [
"johndoe@antartica-base.com",
"gordonfreeman@antartica-base.com"
],
"cci": [
"missepitech@antartica-base.com"
],
"obj": "EpiMTP2017 > all",
"contenu": "Hello 3301 Word ! !",
"date": "21-12-1993",
"domaine": "antartica-base.com",
"state": "0",
"pseudo_f": "Jd"
},
"456": {
"from": "laray.croft@antartica-base.com",
"to": [
"lol@hotmail.fr",
"pipo@antartica-base.com"
],
"cc": [
"johndoe@antartica-base.com",
"gordonfreeman@antartica-base.com"
],
"cci": [
"missepitech@antartica-base.com"
],
"obj": "EpiMTP2017 > all",
"contenu": "Hello 3301 Word ! !",
"date": "21-12-1993",
"domaine": "antartica-base.com",
"state": "0",
"pseudo_f": "Jd"
}
}
main.pro
TEMPLATE = app
TARGET = main
QT = core
SOURCES += main.cpp
main.cpp
#include <QFile>
#include <QByteArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QDebug>
int main()
{
QFile file("main.json");
file.open(QIODevice::ReadOnly | QIODevice::Text);
QByteArray jsonData = file.readAll();
file.close();
QJsonDocument d = QJsonDocument::fromJson(jsonData);
QJsonObject sett2 = d.object();
QJsonValue value = sett2.begin().value();
QJsonValue value2 = sett2.value(sett2.keys().first());
qDebug() << value;
qDebug() << " =========== ";
qDebug() << value2;
return 0;
}
Build and Run
qmake && make && ./main
Output
QJsonValue(object, QJsonObject({"cc": ["johndoe@antartica-base.com","gordonfreeman@antartica-base.com"],"cci": ["missepitech@antartica-base.com"],"contenu": "Hello 3301 Word ! !","date": "21-12-1993","domaine": "antartica-base.com","from": "magellan.dunord@antartica-base.com","obj": "EpiMTP2017 > all","pseudo_f": "Jd","state": "0","to": ["lol@hotmail.fr","pipo@antartica-base.com"]}) )
===========
QJsonValue(object, QJsonObject({"cc": ["johndoe@antartica-base.com","gordonfreeman@antartica-base.com"],"cci": ["missepitech@antartica-base.com"],"contenu": "Hello 3301 Word ! !","date": "21-12-1993","domaine": "antartica-base.com","from": "magellan.dunord@antartica-base.com","obj": "EpiMTP2017 > all","pseudo_f": "Jd","state": "0","to": ["lol@hotmail.fr","pipo@antartica-base.com"]}) )