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.8k 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
    #3

    I just put 10 in the example. I've tried bool, string, double... nothing works. Im using 5.2.1. And did I mention that nothing seems to work and my compiler complains regardless of how i try to assign. there appears to be no way to update the on an already created QJsonValue object... Ive never seen an object that wont let you assign to it as a way of preventing you from assigning the wrong thing. That seems like sewing someones mouth shut to prevent them from eating poison. Just sayin.

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

      But thank you for your response JKSH

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

        Sorry, you asked my compiler: Microsoft Visual C++ 11(amd64) and my kit is Desktop Qt 5.2.1 MSVC2012 OpenGL 64 bit.

        Here's the full source, Ive tried all your recommendations and nothing works:
        @
        void MyConfig::configValueChangedHandler(QString name, bool updateVal){
        QFile configFile("MyConfig.json");
        QString inputData;

        if(configFile.open(QIODevice::ReadWrite | QIODevice::Text))
        {
            inputData = configFile.readAll();
            QJsonDocument jsonDoc = QJsonDocument::fromJson(inputData.toUtf8());
            QJsonObject jo = jsonDoc.object();
            QJsonValue value = jo.value(QString(name));
            //value = value.toBool(updateVal);
        

        // jo.value(QString(name)) = jo.value(name).toBool(updateVal);
        // value = jo.value(QString(name));
        // value = value.operator =(updateVal);
        //value = updateVal;

        // QJsonValue subobj = item[name];
        // subobj. = updateVal;

            //item[name] = updateVal;
            configFile.write(jsonDoc.toJson());
        
            configFile.close();
        }
        

        }
        @

        I've left all the attempts to assign a value back to value in place. Thanks for your help. Still doesnt seem to be a way to assign a value to QJsonValue, or QJsonObject

        EDIT: Enclosed code in '@' tags -- JKSH

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

          Ah, I JUST uninstalled MSVC 2012 from my system yesterday!

          Anyway, I made minor modifications to your code to make a fully-standalone program...
          @
          #include <QFile>
          #include <QJsonDocument>
          #include <QJsonObject>

          int main(int argc, char *argv[])
          {
          QString name;
          bool updateVal;

          QFile configFile&#40;"MyConfig.json"&#41;;
          QString inputData;
          
          if(configFile.open(QIODevice::ReadWrite | QIODevice::Text))
          {
              inputData = configFile.readAll();
              QJsonDocument jsonDoc = QJsonDocument::fromJson(inputData.toUtf8());
              QJsonObject jo = jsonDoc.object();
              QJsonValue value = jo.value(QString(name));
              value = value.toBool(updateVal);
              jo.value(QString(name)) = jo.value(name).toBool(updateVal);
              value = jo.value(QString(name));
              value = value.operator =(updateVal);
              value = updateVal;
          

          // QJsonValue subobj = item[name];
          // subobj = updateVal;

          // item[name] = updateVal;
          configFile.write(jsonDoc.toJson());
          configFile.close();
          }

          return 0;
          

          }
          @

          ...and successfully compiled it with MSVC 2013 64-bit + Qt 5.3.0 beta, as well as MinGW 4.8 32-bit + Qt 5.2.1. The only issues I encountered were warnings about unused variables/parameters.

          There's nothing invalid with your code, as far as I can see.

          Perhaps some binary files simply got corrupted -- Try starting a new project and creating a minimal example.

          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
            #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 Offline
                JKSHJ Offline
                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 Offline
                      JKSHJ Offline
                      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 Offline
                          JKSHJ Offline
                          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 Offline
                              JKSHJ Offline
                              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