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. [SOLVED]QJsonDocument, QJsonValue, QJsonObject: How To Change Value from File
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]QJsonDocument, QJsonValue, QJsonObject: How To Change Value from File

Scheduled Pinned Locked Moved General and Desktop
16 Posts 2 Posters 27.3k 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.
  • D Offline
    D Offline
    doozii
    wrote on last edited by
    #7

    LOL... I swear I tried that too... But Ill give it another shot

    1 Reply Last reply
    0
    • D Offline
      D Offline
      doozii
      wrote on last edited by
      #8

      your code seems like you are doing the same thing over and over... granted Im fairly rusty with my C++ but it seems like 5 lines of code to do where a single @value = updateVal@ should suffice. That doesnt seem efficient to me. Ill let you know if it works

      1 Reply Last reply
      0
      • JKSHJ Online
        JKSHJ Online
        JKSH
        Moderators
        wrote on last edited by
        #9

        [quote author="doozii" date="1395837642"]your code seems like you are doing the same thing over and over... granted Im fairly rusty with my C++ but it seems like 5 lines of code to do where a single @value = updateVal@ should suffice. That doesnt seem efficient to me. Ill let you know if it works[/quote]That's your code -- I simply moved it into main() and un-commented lines.

        My main point was this: There is no compilation error. All of the different ways to assign something to QJsonValue are valid.

        (The actual code I would use is in my first reply, just below your original post)

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

        1 Reply Last reply
        0
        • D Offline
          D Offline
          doozii
          wrote on last edited by
          #10

          ohhh I see. yeah I was trying to just use @value = updateVal;@ I left all the commented out lines in to show what i had tried.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            doozii
            wrote on last edited by
            #11

            So I believe you are correct. I missed your comment about not being able to initialize with pointers and I was passing a pointer. Did not realize it wouldnt work with a reference. I tried again and all of the things I tried seem to work now.

            Thank you so much for working through this with me.

            1 Reply Last reply
            0
            • JKSHJ Online
              JKSHJ Online
              JKSH
              Moderators
              wrote on last edited by
              #12

              I'm glad you've found a solution :)

              All the best with your project!

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

              1 Reply Last reply
              0
              • D Offline
                D Offline
                doozii
                wrote on last edited by
                #13

                I got this to compile but there is no value getting written to the QJSonValue and/or the value isnt getting written back to the QJsonDocument. So when I do the @value = val@ and I look at the value variable... its empty.

                I wish there was example code for this somewhere or someone would write a book and include JSON handling in it.

                1 Reply Last reply
                0
                • JKSHJ Online
                  JKSHJ Online
                  JKSH
                  Moderators
                  wrote on last edited by
                  #14

                  It's not quite a book, but see the official unit tests for a comprehensive suite of JSON assignments/manipulations: http://code.woboq.org/qt5/qtbase/tests/auto/corelib/json/tst_qtjson.cpp.html

                  How did you look at the QJsonValue?

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

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    doozii
                    wrote on last edited by
                    #15

                    i did a qWarning() << value.toString(); and a qWarning() << value; Also tried qWarning() << jo[name] and a host of others. All returned as ""

                    1 Reply Last reply
                    0
                    • JKSHJ Online
                      JKSHJ Online
                      JKSH
                      Moderators
                      wrote on last edited by
                      #16

                      Code:
                      @
                      #include <QJsonDocument>
                      #include <QJsonObject>
                      #include <QFile>
                      #include <QDebug>

                      int main()
                      {
                      QFile file("test.json");
                      if (file.open(QFile::ReadOnly|QFile::Text))
                      {
                      QByteArray jsonText = file.readAll();
                      file.close();

                          QJsonDocument doc = QJsonDocument::fromJson(jsonText&#41;;
                          qDebug() << doc;
                      
                          QJsonObject obj = doc.object();
                          qDebug() << obj;
                      
                          QJsonValue val = obj["dblValue"];
                          qDebug() << val;
                      
                          qDebug() << "-----";
                      
                          val = 1.61803;
                          qDebug() << val;
                      
                          obj["dblValue"] = val;
                          qDebug() << obj;
                      
                          doc.setObject(obj);
                          qDebug() << doc;
                      
                          if (file.open(QFile::WriteOnly|QFile::Text))
                          {
                              file.write(doc.toJson());
                              file.close();
                          }
                      }
                      
                      return 0;
                      

                      }
                      @

                      Input file:
                      @
                      {
                      "boolValue": true,
                      "dblValue": 3.14159,
                      "strValue": "Hello!"
                      }
                      @

                      Output:
                      @
                      QJsonDocument({"boolValue":true,"dblValue":3.1415899999999999,"strValue":"Hello!"})
                      QJsonObject({"boolValue":true,"dblValue":3.1415899999999999,"strValue":"Hello!"})
                      QJsonValue(double, 3.14159)

                      QJsonValue(double, 1.61803)
                      QJsonObject({"boolValue":true,"dblValue":1.6180300000000001,"strValue":"Hello!"})
                      QJsonDocument({"boolValue":true,"dblValue":1.6180300000000001,"strValue":"Hello!"})
                      @

                      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