Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to modify data and save to json file?
QtWS25 Last Chance

How to modify data and save to json file?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
16 Posts 9 Posters 11.2k 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.
  • A Offline
    A Offline
    Alex Dragon
    wrote on last edited by
    #1

    I loaded json file, shown it to listmodel and I want to save data in json file.
    Help me!

    S 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What exactly did you implement ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Alex Dragon

        I loaded json file, shown it to listmodel and I want to save data in json file.
        Help me!

        S Offline
        S Offline
        sharath
        wrote on last edited by
        #3

        Hi @Alex-Dragon ,
        You suppose to use QJsonValueReference to modify the json data.

        {
            "FirstName": "John",
            "LastName": "Doe",
            "Age": 43,
            "Address": {
                "Street": "Downing Street 10",
                "City": "London",
                "Country": "Great Britain"
            },
            "Phone numbers": [
                "+44 1234567",
                "+44 2345678"
            ]
        }
        

        suppose now if you need to change the street name, use the following code.

        QFile file(filepath);
        file.open(QIODevice::ReadOnly | QIODevice::Text);
        QJsonParseError JsonParseError;
        QJsonDocument JsonDocument = QJsonDocument::fromJson(file.readAll(), &JsonParseError);
        file.close();
        QJsonObject RootObject = JsonDocument.object();
        QJsonValueRef ref = RootObject.find("Address").value();
        QJsonObject m_addvalue = ref.toObject();
        m_addvalue.insert("Street","India");//set the value you want to modify
        ref=m_addvalue; //assign the modified object to reference
        JsonDocument.setObject(RootObject); // set to json document
        file.open(QFile::WriteOnly | QFile::Text | QFile::Truncate);
        file.write(JsonDocument.toJson());
        file.close();
        
        
        

        try this and let me know your result.
        Happy coding.

        J 1 Reply Last reply
        4
        • S sharath

          Hi @Alex-Dragon ,
          You suppose to use QJsonValueReference to modify the json data.

          {
              "FirstName": "John",
              "LastName": "Doe",
              "Age": 43,
              "Address": {
                  "Street": "Downing Street 10",
                  "City": "London",
                  "Country": "Great Britain"
              },
              "Phone numbers": [
                  "+44 1234567",
                  "+44 2345678"
              ]
          }
          

          suppose now if you need to change the street name, use the following code.

          QFile file(filepath);
          file.open(QIODevice::ReadOnly | QIODevice::Text);
          QJsonParseError JsonParseError;
          QJsonDocument JsonDocument = QJsonDocument::fromJson(file.readAll(), &JsonParseError);
          file.close();
          QJsonObject RootObject = JsonDocument.object();
          QJsonValueRef ref = RootObject.find("Address").value();
          QJsonObject m_addvalue = ref.toObject();
          m_addvalue.insert("Street","India");//set the value you want to modify
          ref=m_addvalue; //assign the modified object to reference
          JsonDocument.setObject(RootObject); // set to json document
          file.open(QFile::WriteOnly | QFile::Text | QFile::Truncate);
          file.write(JsonDocument.toJson());
          file.close();
          
          
          

          try this and let me know your result.
          Happy coding.

          J Offline
          J Offline
          josht000
          wrote on last edited by
          #4

          @sharath Really awesome code snippet! Saved me lots of time, was exactly what I was looking for, and it worked great. Thanks!!!!

          Pablo J. RoginaP 1 Reply Last reply
          0
          • J josht000

            @sharath Really awesome code snippet! Saved me lots of time, was exactly what I was looking for, and it worked great. Thanks!!!!

            Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #5

            @josht000 please don't forget to mark your post as solved!

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            SGaistS 1 Reply Last reply
            0
            • Pablo J. RoginaP Pablo J. Rogina

              @josht000 please don't forget to mark your post as solved!

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Pablo-J-Rogina it's not the original author of this thread ;-)

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              3
              • Y Offline
                Y Offline
                yashnikam
                wrote on last edited by
                #7

                How to add phone numbers in this code snippet?

                jsulmJ 1 Reply Last reply
                0
                • Y yashnikam

                  How to add phone numbers in this code snippet?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @yashnikam In the same way it is done for address...

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • T Offline
                    T Offline
                    the woft
                    wrote on last edited by
                    #9

                    @SGaist I want to change the value of FirstName and Age , it has no object to find , pls help me !

                    JonBJ 1 Reply Last reply
                    0
                    • T the woft

                      @SGaist I want to change the value of FirstName and Age , it has no object to find , pls help me !

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #10

                      @the-woft
                      FirstName and Age are in the RootObject just like Address is. Their values will be a string and an integer respectively, so retrievable via QString QJsonValue::toString() const & int QJsonValue::toInt(int defaultValue = 0) const and modifiable via QJsonValueRef QJsonObject::operator[](const QString &key).

                      T 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @the-woft
                        FirstName and Age are in the RootObject just like Address is. Their values will be a string and an integer respectively, so retrievable via QString QJsonValue::toString() const & int QJsonValue::toInt(int defaultValue = 0) const and modifiable via QJsonValueRef QJsonObject::operator[](const QString &key).

                        T Offline
                        T Offline
                        the woft
                        wrote on last edited by
                        #11

                        @JonB said in How to modify data and save to json file?:

                        FirstName and Age are in the RootObject just like Address is. Their values will be strings, so retrievable via QString QJsonValue::toString() const and modifiable via QJsonValueRef QJsonObject::operator[](const QString &key).

                        i'm newbie in qt and i try it but not correct , you can give me sample code do it ?

                        jsulmJ 1 Reply Last reply
                        0
                        • T the woft

                          @JonB said in How to modify data and save to json file?:

                          FirstName and Age are in the RootObject just like Address is. Their values will be strings, so retrievable via QString QJsonValue::toString() const and modifiable via QJsonValueRef QJsonObject::operator[](const QString &key).

                          i'm newbie in qt and i try it but not correct , you can give me sample code do it ?

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @the-woft said in How to modify data and save to json file?:

                          and i try it but not correct

                          Then show what you tried.
                          There is example code in documentation and example applications like https://doc.qt.io/qt-6/qtcore-serialization-savegame-example.html

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          T 1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @the-woft said in How to modify data and save to json file?:

                            and i try it but not correct

                            Then show what you tried.
                            There is example code in documentation and example applications like https://doc.qt.io/qt-6/qtcore-serialization-savegame-example.html

                            T Offline
                            T Offline
                            the woft
                            wrote on last edited by
                            #13

                            @jsulm this my code
                            QJsonValue Age = root_object.value("Age");
                            QVariant tmp = Age.toVariant();
                            qlonglong result = root_object.value("Age").toVariant().toLongLong();
                            QJsonValueRef ref = RootObject("result");
                            QJsonObject m_addvalue = ref.toObject();

                            JonBJ 1 Reply Last reply
                            0
                            • T the woft

                              @jsulm this my code
                              QJsonValue Age = root_object.value("Age");
                              QVariant tmp = Age.toVariant();
                              qlonglong result = root_object.value("Age").toVariant().toLongLong();
                              QJsonValueRef ref = RootObject("result");
                              QJsonObject m_addvalue = ref.toObject();

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #14

                              @the-woft
                              You read the Age entry in root_object but where do you set the Age entry in root_object? You don't. I gave you the reference to QJsonValueRef QJsonObject::operator[](const QString &key) to use.

                              T 1 Reply Last reply
                              0
                              • JonBJ JonB

                                @the-woft
                                You read the Age entry in root_object but where do you set the Age entry in root_object? You don't. I gave you the reference to QJsonValueRef QJsonObject::operator[](const QString &key) to use.

                                T Offline
                                T Offline
                                the woft
                                wrote on last edited by
                                #15

                                @JonB was exactly what I was looking for, and it worked great. Thanks!!!!

                                JonBJ 1 Reply Last reply
                                0
                                • T the woft

                                  @JonB was exactly what I was looking for, and it worked great. Thanks!!!!

                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by
                                  #16

                                  @the-woft For other readers you might post the line of code you added to set rootObject["Age"] correctly.

                                  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