parsing json file
-
wrote on 23 Dec 2020, 09:50 last edited by
so i am trying to parse this json file
{ "file": "/uploads/plugin/auto/cv/libViBELPRPlugin.so", "number_instants" : 5, "name": "ViBE-Auto", "JSON_File" : "/uploads/plugin/auto/cv/libViBELPRPlugin.json", "gpu_servers_configurations": [ { "name": "auto-Recognition-Server", "path": "/uploads/plugin/auto/cv/libViBELPRPlugin.so", "JSON_File" : "/uploads/plugin/auto/cv/libViBELPRPlugin.json", "number_instants" : 5, }, { "name": "auto-Detection-Server", "path": "/uploads/plugin/auto/cv/libViBELPRPlugin.so", "JSON_File" : "/uploads/plugin/auto/cv/libViBELPRPlugin.json", "number_instants" : 5, } ] }
using this code i am trying to print the file at the top but it doesnt print anything
QFile file; file.setFileName(inputFileName); file.open(QIODevice::ReadOnly | QIODevice::Text); QString file_values=file.readAll(); file.close(); QJsonDocument parsejson = QJsonDocument::fromJson(file_values.toUtf8()); QJsonObject jsonobject = parsejson.object(); QJsonValue file_name=jsonobject.value(QString("file")); cout<<file_name.toString().toStdString()<<endl;
-
Lifetime Qt Championwrote on 23 Dec 2020, 09:57 last edited by Christian Ehrlicher
"number_instants" : 5,
You json is not valid.
This function has a second parameter which tells you exactly where your json document is wrong.
-
"number_instants" : 5,
You json is not valid.
This function has a second parameter which tells you exactly where your json document is wrong.
wrote on 23 Dec 2020, 10:03 last edited by@Christian-Ehrlicher
i added this and it prints an empty lineif (QJsonParseError::NoError != jsonError.error) { cout<<file_name.toString().toStdString()<<endl; }
-
Lifetime Qt Championwrote on 23 Dec 2020, 10:07 last edited by Christian Ehrlicher
@Dijkstra said in parsing json file:
i added this and it prints an empty line
where? Please show the code. Also why do you print out the filename instead the parser error? And I already showed you a place where your json is wrong.
-
@Dijkstra said in parsing json file:
i added this and it prints an empty line
where? Please show the code. Also why do you print out the filename instead the parser error? And I already showed you a place where your json is wrong.
wrote on 23 Dec 2020, 10:13 last edited byQJsonParseError jsonError; QFile file; file.setFileName(inputFileName); file.open(QIODevice::ReadOnly | QIODevice::Text); QString file_values=file.readAll(); file.close(); QJsonDocument parsejson = QJsonDocument::fromJson(file_values.toUtf8(),&jsonError); QJsonObject jsonobject = parsejson.object(); QJsonValue file_name=jsonobject.value(QString("file")); if (QJsonParseError::NoError != jsonError.error) { cout<<file_name.toString().toStdString()<<endl; }
when i tried to print the error nothing appeared
-
QFile::open() returns a bool which indicates if the file can be opened or not. Please check it.
btw: Covnerting a QByteArray to a QString just to convert it back to a QByteArray two lines below is not needed. Esp. since the conversion from QByteArray to QString may kill your encoding.
-
QJsonParseError jsonError; QFile file; file.setFileName(inputFileName); file.open(QIODevice::ReadOnly | QIODevice::Text); QString file_values=file.readAll(); file.close(); QJsonDocument parsejson = QJsonDocument::fromJson(file_values.toUtf8(),&jsonError); QJsonObject jsonobject = parsejson.object(); QJsonValue file_name=jsonobject.value(QString("file")); if (QJsonParseError::NoError != jsonError.error) { cout<<file_name.toString().toStdString()<<endl; }
when i tried to print the error nothing appeared
wrote on 23 Dec 2020, 10:52 last edited by JonB@Dijkstra
Additional to @Christian-Ehrlicher, you do not check the return result fromQJsonDocument parsejson = QJsonDocument::fromJson()
, so everything after that is useless. This is the most fundamental thing to do when reading in a JSON document. -
QJsonParseError jsonError; QFile file; file.setFileName(inputFileName); file.open(QIODevice::ReadOnly | QIODevice::Text); QString file_values=file.readAll(); file.close(); QJsonDocument parsejson = QJsonDocument::fromJson(file_values.toUtf8(),&jsonError); QJsonObject jsonobject = parsejson.object(); QJsonValue file_name=jsonobject.value(QString("file")); if (QJsonParseError::NoError != jsonError.error) { cout<<file_name.toString().toStdString()<<endl; }
when i tried to print the error nothing appeared
wrote on 23 Dec 2020, 11:41 last edited by Bonnie@Dijkstra said in parsing json file:
when i tried to print the error nothing appeared
No, you didn't.
You just printed the string value of "file", which is obviously an empty string when you get parse error.
So you got an empty line. -
QJsonParseError jsonError; QFile file; file.setFileName(inputFileName); file.open(QIODevice::ReadOnly | QIODevice::Text); QString file_values=file.readAll(); file.close(); QJsonDocument parsejson = QJsonDocument::fromJson(file_values.toUtf8(),&jsonError); QJsonObject jsonobject = parsejson.object(); QJsonValue file_name=jsonobject.value(QString("file")); if (QJsonParseError::NoError != jsonError.error) { cout<<file_name.toString().toStdString()<<endl; }
when i tried to print the error nothing appeared
wrote on 23 Dec 2020, 11:50 last edited by JonB@Dijkstra
And additional (again!) to @Bonnie, you
a. TestjsonError
when it may not be set to anything (read https://doc.qt.io/qt-5/qjsondocument.html#fromJson and stick to the behaviour it documents)
b. Most importantly, you don't test for error till after you have tried fetch further data from a call which [may have] failed, which is pointless.
6/9