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. QJson changing 1 key inside json file
Forum Updated to NodeBB v4.3 + New Features

QJson changing 1 key inside json file

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 294 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 21 Feb 2021, 18:00 last edited by
    #1
    void DeviceData::write(int row, QString entryName, QString Value)
    {
        QFile file(mJsonFile);
    
        file.open(QIODevice::ReadOnly | QIODevice::Text);
        QJsonParseError JsonParseError;
        QJsonDocument JsonDocument = QJsonDocument::fromJson(file.readAll(), &JsonParseError);
        file.close();
    
        QJsonObject RootObject = JsonDocument.object();
        QJsonArray Array = RootObject.find("Device")->toArray();
        RootObject = Array[row].toObject();
        RootObject[entryName] = Value;
        Array[row] = RootObject;
        RootObject["Device"] = Array;
    
        JsonDocument.setObject(RootObject);
        file.open(QFile::WriteOnly | QFile::Text | QFile::Truncate);
        file.write(JsonDocument.toJson());
        file.close();
    }
    

    This changes the line inside the entry it's in OK but for some reason it also adds a totaly new entry at the bottom outside the Device : []

    {
        "Device": [
            {
                "IPA": "studiolights",
                "Icon": "fa_address_book",
                "Name": "Nameless",
                "Status": "online",
                "Type": "Strip",
                "UVLights": false
            },
            {
                "IPA": "vitrineskap",
                "Icon": "fa_asterisk",
                "Name": "Vitrineskap",
                "Status": "online",
                "Type": "Strip",
                "UVLights": false
            }
        ],
        "IPA": "studiolights",
        "Icon": "fa_address_book",
        "Name": "Nameless",
        "Status": "online",
        "Type": "Strip",
        "UVLights": false
    }
    

    what am i doing wrong?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 21 Feb 2021, 18:31 last edited by
      #2

      @Kris-Revi said in QJson changing 1 key inside json file:

      RootObject

      You use this object for two different aspects of your code.

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

      K 1 Reply Last reply 21 Feb 2021, 18:45
      0
      • C Christian Ehrlicher
        21 Feb 2021, 18:31

        @Kris-Revi said in QJson changing 1 key inside json file:

        RootObject

        You use this object for two different aspects of your code.

        K Offline
        K Offline
        Kris Revi
        wrote on 21 Feb 2021, 18:45 last edited by
        #3

        @Christian-Ehrlicher

        void DeviceData::write(int row, QString entryName, QString Value)
        {
            QFile file(mJsonFile);
        
            file.open(QIODevice::ReadOnly | QIODevice::Text);
            QJsonParseError JsonParseError;
            QJsonDocument JsonDocument = QJsonDocument::fromJson(file.readAll(), &JsonParseError);
            file.close();
        
            QJsonObject RootObject = JsonDocument.object();
            QJsonObject RootObject2 = JsonDocument.object();
            QJsonArray Array = RootObject.find("Device")->toArray();
        
            RootObject = Array[row].toObject();
        
            RootObject[entryName] = Value;
            Array[row] = RootObject;
        
            RootObject2["Device"] = Array;
        
            JsonDocument.setObject(RootObject2);
            file.open(QFile::WriteOnly | QFile::Text | QFile::Truncate);
            file.write(JsonDocument.toJson());
            file.close();
        }
        

        i did this! it works but could i've done it better?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 22 Feb 2021, 06:18 last edited by
          #4

          I still don't get what you want. When you look at your code you see that you still add 'Value' to the RootObject for now reason which is the cause for your 'wrong' json

          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
          0

          1/4

          21 Feb 2021, 18:00

          • Login

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