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. Change values in a json file
Qt 6.11 is out! See what's new in the release blog

Change values in a json file

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.9k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hi!
    The only way I found is to read the file, change its contents and write back the whole file. I.e.:

    @ QFile file("filename.json");
    file.open(QIODevice::ReadOnly);
    QByteArray json = file.readAll();
    file.close();
    QJsonDocument doc(QJsonDocument::fromJson(json));
    QJsonObject root(doc.object());
    // change something
    doc.setObject(root);
    file.open(QIODevice::WriteOnly);
    file.write(doc.toJson(QJsonDocument::Indented));
    file.close();
    @

    Is this the recommended way?

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi,

      [quote author="Mark" date="1424095290"]
      The only way I found is to read the file, change its contents and write back the whole file.

      ...

      Is this the recommended way?[/quote]Yes. There's no other way. (But you could store a copy of the contents, so that you don't have to read from the file every time you want to update it)

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0

      • Login

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