[Interest] Save / load file contents on android

ekke ekke at ekkes-corner.org
Tue Aug 31 14:38:12 CEST 2021


Am 31.08.21 um 14:12 schrieb Wilhelm Meier via Interest:
> Hi all,
>
> does anybody have a minimum working example how to load / save json file
> contents (auscii contents) to / from a file on android.
>
> I made several attempts, but I get weird filenames and I'm not able to
> load the stored file.
>
> thx
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest

Hi, some snippets HowTo save a JSON. use QVariantMap for a JSON Object 
or QVariantList for JSON Array   QJsonDocumentjda  =QJsonDocument::fromVariant(myQVariantMap);

QByteArraybuffer=jda.toJson();

saveTestFile("_my_test.json",buffer);
...

voidDataServer::saveTestFile(constQString&fileName,constQByteArray&buffer)
{
QStringfilePath=mDataManager->dataPath(fileName);
QFilesaveFile(filePath);
if(!saveFile.open(QIODevice::WriteOnly)){
qWarning()<<"Couldn't open file to write "<<filePath;
return;
}
qint64bytesWritten=saveFile.write(buffer);
saveFile.close();
qDebug()<<"Bytes written: "<<bytesWritten<<" to: "<<filePath;
}
....

//Android:AppDataLocationworksoutofthebox,iOSyoumustcreatetheDIRfirst!!
mDataRoot=QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).value(0);
mDataPath=mDataRoot+"/data/";
----
and reading is similar

jda=QJsonDocument::fromJson(dataFile.readAll());
dataFile.close();
if(!jda.isArray()){
myMap =jda.toVariant().toMap();
} else {
  	myList=jda.toVariant().toList();
     }
-----
hope it helps

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20210831/ecd8c14e/attachment.html>


More information about the Interest mailing list