Solved Read Json file
-
i have tried to read a JSON file, but it does not work properly (I'm propbably doing something wrong here).
Note: file.open functions returns true, so the path is correct.
PROBLEM -> it returns no data : jsonData is empty and array is empty
here is my json
{ "notes": [{ "title": "title1", "content": "content1" }, { "title": "title2", "content": "content2" }] }
here is my function
QJsonArray NotesManager::getNotesFromJson() const{ QFile file("notes.json"); file.open(QIODevice::ReadOnly | QIODevice::Text); //returns true QByteArray jsonData = file.readAll(); //jsonData is empty file.close(); QJsonDocument document = QJsonDocument::fromJson(jsonData); QJsonObject object = document.object(); QJsonValue value = object.value("notes"); return value.toArray(); // returns array with no elements }
-
hi @zer0 and welcome
You didn't check, if the file exists before attempting to open it. If the file does not exist, that calling open will create it. IIRC
SO your path is most likely wrong
-
thanks, you are right.