Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt Json only adds 1 new entry eachtime code is called
Forum Update on Monday, May 27th 2025

Qt Json only adds 1 new entry eachtime code is called

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 229 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kris Revi
    wrote on last edited by
    #1
        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

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved