Important: Please read the Qt Code of Conduct -

How to modify json data with out changing postion of keys , needs to change only values?

  • i want to modify only values for this json data?please any one can help me

  • @Raji
    JSON has no ordering of keys. The order keys come out when serializing to text is "random"/"undefined", and there is nothing you can do to affect that. If they come out in the same order each time, and that suits you, you are "lucky". If anything relies on the order, it's wrong.

    To modify values from a JSON text file, you must read it in, parse it into JSON object tree, make your changes in-memory, and export the whole thing back to file. There is no other way.

  • @JonB :ok fine thanks,
    how can i refer/change Param variables inside the Connections,in my above json file

    for connections i am using this format
    QJsonObject RootObject = JsonDocument.object();
    QJsonValueRef ref = RootObject.find("connection").value();
    QJsonObject m_addvalue = ref.toObject();
    m_addvalue.insert("type",mType);/ /set the value you want to modify
    ref=m_addvalue; //assign the modified object to reference

  • @Raji
    As usual (for me), I don't understand the question! :( You change values in the JSON document either by using QJsonValueRef calls

    QJsonValueRef is a helper class for QJsonArray and QJsonObject. When you get an object of type QJsonValueRef, you can use it as if it were a reference to a QJsonValue. If you assign to it, the assignment will apply to the element in the QJsonArray or QJsonObject from which you got the reference.

    or by updating your "back-end" object representation and regenerating the whole JSON document as you did to produce the saved JSON file in the first place.

Log in to reply