Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QJson changing 1 key inside json file



  • 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?


  • Lifetime Qt Champion

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

    RootObject

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



  • @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?


  • Lifetime Qt Champion

    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


Log in to reply