Results 1 to 5 of 5

Thread: QJsonDocument: How to replace a single object / or add it if it doesn't exists?

  1. #1
    Join Date
    Feb 2015
    Posts
    14
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default QJsonDocument: How to replace a single object / or add it if it doesn't exists?

    Hi there,

    lets say there is a JSON file with more than 100 objects, build with QJsonObject, QJsonDocument and QFile:

    {
    "object1": {
    "name": "whatever",
    "size": "1"
    },
    "object6": {
    "name": "whatever",
    "size": "1"
    }
    "object23": {
    "name": "whatever",
    "size": "1"
    }
    and so forth...
    }

    With Qt 5.6 what's the easiest way to replace a single object (for example "object6") / or insert one if it doesn't exist (for example "object4")?

    I got no problem to write the hole JSON file with a replaced or inserted new object again. But is it possible to just write / replace a single object?
    Last edited by mireiner; 31st March 2016 at 18:40.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QJsonDocument: How to replace a single object / or add it if it doesn't exists?

    You are manipulating a memory representation of the information in the file that you can then write to a file if you choose. You read the whole file into that structure, and write a whole file from that structure.

    For your example document:
    • Load the JSON text from a file into a QJsonDocument
    • Get a copy of the outer QJsonObject from the document
    • Use QJsonObject::remove() and QJsonObject::insert() to manipulate the QJsonObject
    • Construct a new QJsonDocument from the modified QJsonObject
    • Convert the document to JSON and save to a file

  3. #3
    Join Date
    Feb 2015
    Posts
    14
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QJsonDocument: How to replace a single object / or add it if it doesn't exists?

    Hi Chris,

    thank you for your help. I did have a look at QJsonObject::insert() before. But can this function really insert / overwrite a whole JSON Object? It seamed to me it is only capable to insert a single key/value pair inside an object?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QJsonDocument: How to replace a single object / or add it if it doesn't exists?

    Yes, they insert() or remove() single key/value pairs from a QJsonObject. Your example document is a single JSON object containing three key/value pairs where each value is another JSON object. You asked abput inserting a new "object4" key and removing "object6" key from that single object.

  5. #5
    Join Date
    Feb 2015
    Posts
    14
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QJsonDocument: How to replace a single object / or add it if it doesn't exists?

    Hi Chris,

    sorry I missunderstood the key/value parameter of 'insert(const QString &key, const QJsonValue &value)'. I thought it means you only can insert key / value pairs like "size" : "1" in the example above. And overlooked that the parameter 'value' also can be a whole JSON object.

    Now it works. Thanks for helping!

Similar Threads

  1. Replies: 1
    Last Post: 25th December 2015, 13:47
  2. QJsonDocument toJson(QJsonDocument::Compact) not working
    By elmar.wieser in forum Qt Programming
    Replies: 2
    Last Post: 9th January 2015, 07:23
  3. QSignalMapper One single Object
    By cafu1007 in forum Qt Programming
    Replies: 7
    Last Post: 18th April 2011, 09:46
  4. Replies: 2
    Last Post: 21st February 2011, 15:52
  5. Replies: 2
    Last Post: 20th April 2010, 22:27

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.