[SOLVED] Empty QJsonDocument from file
-
Look at the second parameter of "fromJson":http://qt-project.org/doc/qt-5.0/qtcore/qjsondocument.html#fromJson
From there you can get the error
-
Could it be your json file encoding that is problematic ?
-
If you are using QtCreator you could check the editor settings to ensure that it uses UTF8
-
You're welcome !
If everything is working now, please update the thread's title to solved so other forum users may know a solution has been found :)
-
Hi,
I have the same problem, but the solution isn't the UTF8 coding, for me.
QtCreator is already configured to UTF, and that
@ QString jsonString = QString::fromUtf8(file.readAll());QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toUtf8());
@
don't work for me.My JSON file is built and written by JsonDocument and QFile, so, it could be valide:
@
bool JSONStorage::storeJSONFile(){
if( !jsonFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate) ){
return false;
}
QJsonDocument jsonDocument( jsonArray );
jsonFile.write( jsonDocument.toJson() );
jsonFile.close();
return true;
}
@But I do not manage to load this file. I use this method:
@
void JSONStorage::openJSONFile(){
QJsonParseError parseErr;
QJsonDocument jsonDocument;if( !jsonFile.open(QIODevice::ReadOnly | QIODevice::Text) ){ return; } jsonDocument.fromJson( jsonFile.readAll(), &parseErr ); qDebug() << parseErr.errorString(); if( jsonDocument.isEmpty()){ qDebug() << "Empty!"; } if( jsonDocument.isArray() ){ qDebug() << "Is array"; jsonArray = jsonDocument.array(); }
}
@It says that is empty, and parseErr: "no error occurred"
If I write:
@
qDebug() << jsonFile.readAll();
@I can see my document. It isn't empty!
What is wrong?
-
Use the second parameter of fromJson(const QByteArray & json, QJsonParseError * error = 0) and check what it returns
-
Indeed, fromJson is a static function