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?
Servers for Qt installer are currently down

How to modify data and save to json file?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
16 Posts 9 Posters 11.3k Views 2 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.
  • S Offline
    S Offline
    SGaist
    Lifetime Qt Champion
    wrote on 18 Mar 2019, 22:47 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
      18 Mar 2019, 04:35

      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 21 Mar 2019, 06:53 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 11 Apr 2020, 19:45
      4
      • S sharath
        21 Mar 2019, 06:53

        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 11 Apr 2020, 19:45 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!!!!

        P 1 Reply Last reply 11 Apr 2020, 20:11
        0
        • J josht000
          11 Apr 2020, 19:45

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

          P Offline
          P Offline
          Pablo J. Rogina
          wrote on 11 Apr 2020, 20:11 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

          S 1 Reply Last reply 12 Apr 2020, 07:18
          0
          • P Pablo J. Rogina
            11 Apr 2020, 20:11

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

            S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 12 Apr 2020, 07:18 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 10 Apr 2021, 12:53 last edited by
              #7

              How to add phone numbers in this code snippet?

              J 1 Reply Last reply 12 Apr 2021, 08:07
              0
              • Y yashnikam
                10 Apr 2021, 12:53

                How to add phone numbers in this code snippet?

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 12 Apr 2021, 08:07 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 8 Jun 2023, 10:08 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 8 Jun 2023, 11:18
                  0
                  • T the woft
                    8 Jun 2023, 10:08

                    @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 8 Jun 2023, 11:18 last edited by JonB 6 Sept 2023, 06:30
                    #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 9 Jun 2023, 03:46
                    0
                    • JonBJ JonB
                      8 Jun 2023, 11:18

                      @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 9 Jun 2023, 03:46 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 ?

                      J 1 Reply Last reply 9 Jun 2023, 06:08
                      0
                      • T the woft
                        9 Jun 2023, 03:46

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

                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 9 Jun 2023, 06:08 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 9 Jun 2023, 07:56
                        0
                        • J jsulm
                          9 Jun 2023, 06:08

                          @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 9 Jun 2023, 07:56 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 9 Jun 2023, 08:03
                          0
                          • T the woft
                            9 Jun 2023, 07:56

                            @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 9 Jun 2023, 08:03 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 9 Jun 2023, 09:19
                            0
                            • JonBJ JonB
                              9 Jun 2023, 08:03

                              @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 9 Jun 2023, 09:19 last edited by
                              #15

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

                              JonBJ 1 Reply Last reply 9 Jun 2023, 09:22
                              0
                              • T the woft
                                9 Jun 2023, 09:19

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

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on 9 Jun 2023, 09:22 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