[SOLVED]QJsonDocument, QJsonObject, are showing NULL and are not loading data correctly
-
I'm quite new to QT development and I hope someone can help. I found the QT reference seriously lacking in examples for QJsonDocument and QJsonObject, etc. The only fairly decent example was found on StackOverflow at http://stackoverflow.com/questions/15893040/how-to-create-read-write-json-files-in-qt5 but when I tried this example, my QJsonObjects were showing as NULL. Does anyone know where I can find a full example or a explanation of what Im doing wrong? I have copied and pasted the example here in case this forum disallows links.
{
"appDesc": {
"description": "SomeDescription",
"message": "SomeMessage"
},
"appName": {
"description": "Home",
"message": "Welcome",
"imp":["awesome","best","good"]
}
}void readJson()
{
QString val;
QFile file;
file.setFileName("test.json");
file.open(QIODevice::ReadOnly | QIODevice::Text);
val = file.readAll();
file.close();
qWarning() << val;
QJsonDocument d = QJsonDocument::fromJson(val.toUtf8());
QJsonObject sett2 = d.object();
QJsonValue value = sett2.value(QString("appName"));
qWarning() << value;
QJsonObject item = value.toObject();
qWarning() << tr("QJsonObject of description: ") << item;/* incase of string value get value and convert into string*/ qWarning() << tr("QJsonObject[appName] of description: ") << item["description"]; QJsonValue subobj = item["description"]; qWarning() << subobj.toString(); /* incase of array get array and convert into string*/ qWarning() << tr("QJsonObject[appName] of value: ") << item["imp"]; QJsonArray test = item["imp"].toArray(); qWarning() << test[1].toString();
}
I've been trying to step through these lines:
QJsonDocument d = QJsonDocument::fromJson(val.toUtf8());
QJsonObject sett2 = d.object();
QJsonValue value = sett2.value(QString("appName"));
qWarning() << value;
QJsonObject item = value.toObject();and for each object, Im seeing nothing in them. I have verified that my QString does contain data.
Thanks!
-
Hi and welcome to devnet,
Please enclose your code with coding tags (one @ at the beginning and one at the end) It will make your code readable and allow to more easily help you debug your situation
-
@/* test.json */
{
"appDesc": {
"description": "SomeDescription",
"message": "SomeMessage"
},
"appName": {
"description": "Home",
"message": "Welcome",
"imp":["awesome","best","good"]
}
}void readJson()
{
QString val;
QFile file;
file.setFileName("test.json");
file.open(QIODevice::ReadOnly | QIODevice::Text);
val = file.readAll();
file.close();
qWarning() << val;
QJsonDocument d = QJsonDocument::fromJson(val.toUtf8());
QJsonObject sett2 = d.object();
QJsonValue value = sett2.value(QString("appName"));
qWarning() << value;
QJsonObject item = value.toObject();
qWarning() << tr("QJsonObject of description: ") << item;/* incase of string value get value and convert into string*/ qWarning() << tr("QJsonObject[appName] of description: ") << item["description"]; QJsonValue subobj = item["description"]; qWarning() << subobj.toString(); /* incase of array get array and convert into string*/ qWarning() << tr("QJsonObject[appName] of value: ") << item["imp"]; QJsonArray test = item["imp"].toArray(); qWarning() << test[1].toString();
}@
-
-
You can ask for a documentation update on "the bug report system":http://bugreports.qt-project.org using a feature request.
Just to be sure, did you got that example working properly ?
-
no still not working... unable to find a working example and so far no clue as to what I'm doing wrong. Thanks for the heads up on the bug system. Any chance you can tell em whats wrong with the code example? Or provide an example that works? Ste-by-step would be nice. Thanks
-
I ran just this part:
@ file.setFileName("test.json");
file.open(QIODevice::ReadOnly | QIODevice::Text);
val = file.readAll();
file.close();
qWarning() << val;
QJsonDocument d = QJsonDocument::fromJson(val.toUtf8());
qWarning << d.isEmpty();
@
and received true.. in other words the document is not getting populated from the json. Is this a known issue? -
AFAIK, it should not be a problem. I've run the example and it's working fine. However what may happen is that your application can't find the file. You don't check that it's been opened successfully. Using a relative path, you must ensure that the file is besides your application or just give the absolute path to your json file
-
You might have had some stray non printable character in the JSON.
Anyway, glad it's working now.
Can you please update the thread title prepending [solved] ? So other forum users may know a solution has been found :)
-
Just edit your first post and modify the title