[SOLVED]QJsonDocument, QJsonObject, are showing NULL and are not loading data correctly
-
wrote on 11 Mar 2014, 02:25 last edited by
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
-
wrote on 11 Mar 2014, 17:56 last edited by
@/* 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();
}@
-
wrote on 11 Mar 2014, 17:58 last edited by
That is the example Im using but there are no values getting set in wither the QJsonDocument, the QJsonObject called sett2 or any combination of QJsonValue objects
-
wrote on 11 Mar 2014, 18:00 last edited by
And I have the json in its own file called test.json that is being opened just as in the example. I created the json file by using the Add New > General > Text File and giving it a .json file extension.
-
wrote on 11 Mar 2014, 18:02 last edited by
And the abve is the ONLY decent example Ive been able to find as I scour Google and other searches... Is there any way for the QT Team to create some examples? Examples for other basics would be great too for those of us who are completely new to QT.
-
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 ?
-
wrote on 12 Mar 2014, 03:07 last edited by
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
-
wrote on 12 Mar 2014, 05:07 last edited by
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? -
wrote on 12 Mar 2014, 05:12 last edited by
Ok, I figured it out. Apparently, indenting your JSON file causes problems and the QJsonDocument can not populate. I removed all the spaces and test.json formatting for readability and its now populating. Thanks all
-
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
-
wrote on 12 Mar 2014, 11:43 last edited by
Nah, it was finding the file just fine and was reading into my QString just fine, but apparently it wasnt liking spaces or newlines or something cause it sure enough wasnt populating. But its great now. Doing exactly what I want. Thanks again
-
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 :)
-
wrote on 12 Mar 2014, 20:37 last edited by
I dont see a way to resolve this as solved
-
Just edit your first post and modify the title
1/15