[SOLVED]QJsonDocument, QJsonObject, are showing NULL and are not loading data correctly
-
@/* 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