parsing json file
-
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;
-
"number_instants" : 5,
You json is not valid.
This function has a second parameter which tells you exactly where your json document is wrong.
-
@Christian-Ehrlicher
i added this and it prints an empty lineif (QJsonParseError::NoError != jsonError.error) { cout<<file_name.toString().toStdString()<<endl; }
-
@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.
-
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
-
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.
-
@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. -
-
@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.