Qt Json only adds 1 new entry eachtime code is called
Unsolved
General and Desktop
-
QJsonParseError error; QJsonDocument inputJsonDoc = QJsonDocument::fromJson(file.readAll(), &error); file.close(); QJsonObject newDevice; newDevice.insert("IPA", "newdevice"); newDevice.insert("Icon", "fa_bolt"); newDevice.insert("Name", "New Device"); newDevice.insert("Status", "online"); newDevice.insert("Type", "Strip"); newDevice.insert("UVLights", "false");
after this i do
QJsonArray inputJsonArray = inputJsonDoc.array(); // create JsonArray and store the inputJson as an array inputJsonArray.push_back(newDevice); // then i push_back the newDevice into the array QJsonObject newJsonObject; // make a new JsonObject newJsonObject["Devices"] = inputJsonArray; // and store the new Json Array inside "Devices" object QJsonDocument outputJsonDoc(newJsonObject); // then put everything back into a JsonDocument file.write(outputJsonDoc.toJson()); // and write back to the file
right? :S why is this not working for me?
this is the json file befor adding
{ "Devices": [ ] }
after i add 1 entry
{ "Devices": [ { "IPA": "newdevice", "Icon": "fa_bolt", "Name": "New Device", "Status": "online", "Type": "Strip", "UVLights": "false" } ] }
after adding 1 more
{ "Devices": [ { "IPA": "newdevice", "Icon": "fa_bolt", "Name": "New Device", "Status": "online", "Type": "Strip", "UVLights": "false" } ] }
it only adds 1 entry :S it should keep adding a new everytime :S
-
You read a QJsonArray but store a QJsonObject - how is this suppose to work? As we already told you somewhere else you should check the type of your QJsonValue/QJsonDocument via https://doc.qt.io/qt-5/qjsondocument.html#isArray