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. Can't convert from QJsonValue to int.

Can't convert from QJsonValue to int.

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 4 Posters 4.1k 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.
  • H Offline
    H Offline
    HenkCoder
    wrote on last edited by
    #1

    Hello I have a problem and I don't know how to solve it.
    This is the JSON file:

     "toolBarColor": {
            "red": 156,
            "green": 244,
            "blue": 230
        },
    

    and this my Qt code:

    QFile file(QCoreApplication::applicationDirPath() + "/settings.json");
        QString input;
        file.open(QIODevice::ReadOnly | QIODevice::Text);
        input = file.readAll();
        QJsonDocument jsonDoc{QJsonDocument::fromJson(input.toUtf8())};
        file.close();
        QJsonObject jo{jsonDoc.object()};
        QJsonValue tempVal{jo.value("toolBarColor")};
        QJsonObject valueObj{tempVal.toObject()};
        QJsonValue redVal{valueObj.value("red")};
        QJsonValue greenVal{valueObj.value("green")};
        QJsonValue blueVal{valueObj.value("blue")};
        int red{redVal.toInt()};
        int green{greenVal.toInt()};
        int blue{blueVal.toInt()};
    

    I don't know why, but this returns just the default value 0.
    Can you please explain me how to solve this?

    JonBJ Christian EhrlicherC 2 Replies Last reply
    0
    • Christian EhrlicherC Christian Ehrlicher

      @HenkCoder said in Can't convert from QJsonValue to int.:

      I'm so dumb.

      Please use the second parameter from QJsonDocument::fromJson() to get noticed about parser errors.

      ...

      H Offline
      H Offline
      HenkCoder
      wrote on last edited by
      #21

      @Christian-Ehrlicher I used that but the problem was that the file contained a JSON array first

      1 Reply Last reply
      0
      • JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by JoeCFD
        #2

        You may always need to do the following in your code:

        if ( false == file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            print out open failed.
            return.
        }
        
        input = file.readAll();
        if ( input.isEmpty() ) {
            print out empty file message
            return;
        }
        
        if ( true == jsonDoc.isEmpty() ) {
            print out message
            return
        }
        
        
        H 1 Reply Last reply
        0
        • JoeCFDJ JoeCFD

          You may always need to do the following in your code:

          if ( false == file.open(QIODevice::ReadOnly | QIODevice::Text)) {
              print out open failed.
              return.
          }
          
          input = file.readAll();
          if ( input.isEmpty() ) {
              print out empty file message
              return;
          }
          
          if ( true == jsonDoc.isEmpty() ) {
              print out message
              return
          }
          
          
          H Offline
          H Offline
          HenkCoder
          wrote on last edited by
          #3

          @JoeCFD It does open properly and I have added that place of code but it still does this thing

          1 Reply Last reply
          0
          • H HenkCoder

            Hello I have a problem and I don't know how to solve it.
            This is the JSON file:

             "toolBarColor": {
                    "red": 156,
                    "green": 244,
                    "blue": 230
                },
            

            and this my Qt code:

            QFile file(QCoreApplication::applicationDirPath() + "/settings.json");
                QString input;
                file.open(QIODevice::ReadOnly | QIODevice::Text);
                input = file.readAll();
                QJsonDocument jsonDoc{QJsonDocument::fromJson(input.toUtf8())};
                file.close();
                QJsonObject jo{jsonDoc.object()};
                QJsonValue tempVal{jo.value("toolBarColor")};
                QJsonObject valueObj{tempVal.toObject()};
                QJsonValue redVal{valueObj.value("red")};
                QJsonValue greenVal{valueObj.value("green")};
                QJsonValue blueVal{valueObj.value("blue")};
                int red{redVal.toInt()};
                int green{greenVal.toInt()};
                int blue{blueVal.toInt()};
            

            I don't know why, but this returns just the default value 0.
            Can you please explain me how to solve this?

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

            @HenkCoder said in Can't convert from QJsonValue to int.:

            QJsonValue tempVal{jo.value("toolBarColor")};
            QJsonObject valueObj{tempVal.toObject()};
            

            Check what these two lines set. Easy to do in debugger. And while you're there look in QJsonValue redVal{valueObj.value("red")}; too!

            P.S.
            Actually don't bother. You have not put any error checking in (you always should), you haven't checked the very top-level

            QJsonDocument jsonDoc{QJsonDocument::fromJson(input.toUtf8())};
            

            You should find that is returning an empty document (and setting an error for you)? Assuming that is your file verbatim you have a trailing , (comma) right at the end. That is not legal: this is JSON, not C++! :)

            1 Reply Last reply
            0
            • H HenkCoder

              Hello I have a problem and I don't know how to solve it.
              This is the JSON file:

               "toolBarColor": {
                      "red": 156,
                      "green": 244,
                      "blue": 230
                  },
              

              and this my Qt code:

              QFile file(QCoreApplication::applicationDirPath() + "/settings.json");
                  QString input;
                  file.open(QIODevice::ReadOnly | QIODevice::Text);
                  input = file.readAll();
                  QJsonDocument jsonDoc{QJsonDocument::fromJson(input.toUtf8())};
                  file.close();
                  QJsonObject jo{jsonDoc.object()};
                  QJsonValue tempVal{jo.value("toolBarColor")};
                  QJsonObject valueObj{tempVal.toObject()};
                  QJsonValue redVal{valueObj.value("red")};
                  QJsonValue greenVal{valueObj.value("green")};
                  QJsonValue blueVal{valueObj.value("blue")};
                  int red{redVal.toInt()};
                  int green{greenVal.toInt()};
                  int blue{blueVal.toInt()};
              

              I don't know why, but this returns just the default value 0.
              Can you please explain me how to solve this?

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @HenkCoder said in Can't convert from QJsonValue to int.:

              This is the JSON file:
              "toolBarColor": {
              "red": 156,
              "green": 244,
              "blue": 230
              },

              This is not a valid json file. Please use the second parameter from QJsonDocument::fromJson() to get noticed about parser errors.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              JoeCFDJ H 2 Replies Last reply
              3
              • JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by JoeCFD
                #6

                My following code works

                QJsonObject jo{jsonDoc.object()};
                JsonObject valueObj{jo["toolBarColor"]};
                QJsonValue redVal{valueObj.value("red")};
                QJsonValue greenVal{valueObj.value("green")};
                QJsonValue blueVal{valueObj.value("blue")};
                
                JonBJ H 2 Replies Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @HenkCoder said in Can't convert from QJsonValue to int.:

                  This is the JSON file:
                  "toolBarColor": {
                  "red": 156,
                  "green": 244,
                  "blue": 230
                  },

                  This is not a valid json file. Please use the second parameter from QJsonDocument::fromJson() to get noticed about parser errors.

                  JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by
                  #7

                  @Christian-Ehrlicher You are right. It should be like this
                  {
                  "toolBarColor": {
                  "red": 156,
                  "green": 244,
                  "blue": 230
                  },
                  }

                  1 Reply Last reply
                  0
                  • JoeCFDJ JoeCFD

                    My following code works

                    QJsonObject jo{jsonDoc.object()};
                    JsonObject valueObj{jo["toolBarColor"]};
                    QJsonValue redVal{valueObj.value("red")};
                    QJsonValue greenVal{valueObj.value("green")};
                    QJsonValue blueVal{valueObj.value("blue")};
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #8

                    @JoeCFD said in Can't convert from QJsonValue to int.:

                    JsonObject valueObj{jo["toolBarColor"]};
                    

                    That is treating it as a list of objects or as an object with attribute toolBarColor, I can't recall which. I still think you are "lucky" it parses, as (so far as I know) you are not supposed to end any JSON list of things with a trailing comma....

                    You have posted your last, and I still think you're not supposed to have the

                        },
                    }
                    

                    ... :)

                    JoeCFDJ 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      @HenkCoder said in Can't convert from QJsonValue to int.:

                      This is the JSON file:
                      "toolBarColor": {
                      "red": 156,
                      "green": 244,
                      "blue": 230
                      },

                      This is not a valid json file. Please use the second parameter from QJsonDocument::fromJson() to get noticed about parser errors.

                      H Offline
                      H Offline
                      HenkCoder
                      wrote on last edited by
                      #9

                      @Christian-Ehrlicher Yes, I know, this is the full file:

                      {
                          "font": {
                              "family": "Calibri", 
                              "pointSize": 11
                          },
                      
                          "toolBarColor": {
                              "red": 156,
                              "green": 244,
                              "blue": 230
                          },
                          
                          "oldSplashscreen": false,
                          "darkTheme": false
                      }
                      
                      JonBJ 1 Reply Last reply
                      0
                      • H Offline
                        H Offline
                        HenkCoder
                        wrote on last edited by
                        #10
                        This post is deleted!
                        1 Reply Last reply
                        0
                        • JoeCFDJ JoeCFD

                          My following code works

                          QJsonObject jo{jsonDoc.object()};
                          JsonObject valueObj{jo["toolBarColor"]};
                          QJsonValue redVal{valueObj.value("red")};
                          QJsonValue greenVal{valueObj.value("green")};
                          QJsonValue blueVal{valueObj.value("blue")};
                          
                          H Offline
                          H Offline
                          HenkCoder
                          wrote on last edited by HenkCoder
                          #11

                          @JoeCFD I tried this code and it does not work either,
                          I have another line of code which sets the text of a textedit to red, green and blue variables and it still prints out "000".

                          1 Reply Last reply
                          0
                          • JonBJ JonB

                            @JoeCFD said in Can't convert from QJsonValue to int.:

                            JsonObject valueObj{jo["toolBarColor"]};
                            

                            That is treating it as a list of objects or as an object with attribute toolBarColor, I can't recall which. I still think you are "lucky" it parses, as (so far as I know) you are not supposed to end any JSON list of things with a trailing comma....

                            You have posted your last, and I still think you're not supposed to have the

                                },
                            }
                            

                            ... :)

                            JoeCFDJ Offline
                            JoeCFDJ Offline
                            JoeCFD
                            wrote on last edited by
                            #12

                            @JonB const QJsonValue QJsonDocument::operator[](const QString &key) const
                            https://doc.qt.io/qt-5/qjsondocument.html#operator-5b-5d

                            JonBJ 1 Reply Last reply
                            0
                            • H HenkCoder

                              @Christian-Ehrlicher Yes, I know, this is the full file:

                              {
                                  "font": {
                                      "family": "Calibri", 
                                      "pointSize": 11
                                  },
                              
                                  "toolBarColor": {
                                      "red": 156,
                                      "green": 244,
                                      "blue": 230
                                  },
                                  
                                  "oldSplashscreen": false,
                                  "darkTheme": false
                              }
                              
                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #13

                              @HenkCoder
                              Then if that is the full file naturally you must access via jo["toolBarColor"].

                              Note that each time you use methods like QJsonValue::toInt(), and the other QJsonValue::to...() methods, you will get no error if the value is not of the type you think it is. Use the QJsonValue::is...() methods to check, QJsonValue::type() is your friend to tell you what you actually have.

                              H 2 Replies Last reply
                              0
                              • JoeCFDJ JoeCFD

                                @JonB const QJsonValue QJsonDocument::operator[](const QString &key) const
                                https://doc.qt.io/qt-5/qjsondocument.html#operator-5b-5d

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

                                @JoeCFD said in Can't convert from QJsonValue to int.:

                                @JonB const QJsonValue QJsonDocument::operator[](const QString &key) const
                                https://doc.qt.io/qt-5/qjsondocument.html#operator-5b-5d

                                I know this. Sorry, don't see the relevance.

                                1 Reply Last reply
                                0
                                • JonBJ JonB

                                  @HenkCoder
                                  Then if that is the full file naturally you must access via jo["toolBarColor"].

                                  Note that each time you use methods like QJsonValue::toInt(), and the other QJsonValue::to...() methods, you will get no error if the value is not of the type you think it is. Use the QJsonValue::is...() methods to check, QJsonValue::type() is your friend to tell you what you actually have.

                                  H Offline
                                  H Offline
                                  HenkCoder
                                  wrote on last edited by
                                  #15

                                  @JonB Hello, I tried and there is no isInt() method to verify it, I don't really know now

                                  JonBJ 1 Reply Last reply
                                  0
                                  • JonBJ JonB

                                    @HenkCoder
                                    Then if that is the full file naturally you must access via jo["toolBarColor"].

                                    Note that each time you use methods like QJsonValue::toInt(), and the other QJsonValue::to...() methods, you will get no error if the value is not of the type you think it is. Use the QJsonValue::is...() methods to check, QJsonValue::type() is your friend to tell you what you actually have.

                                    H Offline
                                    H Offline
                                    HenkCoder
                                    wrote on last edited by
                                    #16

                                    @JonB to access that thing I did exactly what you said.

                                    QJsonObject valueObj{jo["toolBarColor"].toObject()};
                                        QJsonValue redVal{valueObj.value("red")};
                                        QJsonValue greenVal{valueObj.value("green")};
                                        QJsonValue blueVal{valueObj.value("blue")};
                                    

                                    this is the code but i don't know if it is right.

                                    Christian EhrlicherC 1 Reply Last reply
                                    0
                                    • H HenkCoder

                                      @JonB to access that thing I did exactly what you said.

                                      QJsonObject valueObj{jo["toolBarColor"].toObject()};
                                          QJsonValue redVal{valueObj.value("red")};
                                          QJsonValue greenVal{valueObj.value("green")};
                                          QJsonValue blueVal{valueObj.value("blue")};
                                      

                                      this is the code but i don't know if it is right.

                                      Christian EhrlicherC Offline
                                      Christian EhrlicherC Offline
                                      Christian Ehrlicher
                                      Lifetime Qt Champion
                                      wrote on last edited by Christian Ehrlicher
                                      #17

                                      Your code from the first post works fine for me. Make sure you're opening the correct json file. The conversion from/to QString is useless and wrong on non utf-8 systems though.

                                      int main(int argc, char *argv[])
                                      {
                                        QCoreApplication app(argc, argv);
                                      
                                        QByteArray content = R"(
                                      {
                                          "font": {
                                              "family": "Calibri",
                                              "pointSize": 11
                                          },
                                      
                                          "toolBarColor": {
                                              "red": 156,
                                              "green": 244,
                                              "blue": 230
                                          },
                                      
                                          "oldSplashscreen": false,
                                          "darkTheme": false
                                      }
                                      )";
                                        QJsonDocument jsonDoc{QJsonDocument::fromJson(content)};
                                        QJsonObject jo{jsonDoc.object()};
                                        QJsonValue tempVal{jo.value("toolBarColor")};
                                        QJsonObject valueObj{tempVal.toObject()};
                                        QJsonValue redVal{valueObj.value("red")};
                                        QJsonValue greenVal{valueObj.value("green")};
                                        QJsonValue blueVal{valueObj.value("blue")};
                                        int red{redVal.toInt()};
                                        int green{greenVal.toInt()};
                                        int blue{blueVal.toInt()};
                                        qDebug() << red << green << blue;   // prints 156 244 230
                                        return 0;
                                      }
                                      
                                      

                                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                      Visit the Qt Academy at https://academy.qt.io/catalog

                                      H 1 Reply Last reply
                                      0
                                      • H HenkCoder

                                        @JonB Hello, I tried and there is no isInt() method to verify it, I don't really know now

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

                                        @HenkCoder said in Can't convert from QJsonValue to int.:

                                        @JonB Hello, I tried and there is no isInt() method to verify it, I don't really know now

                                        So I said use QJsonValue::Type QJsonValue::type() const to check what type is in a QJsonValue before you assume you know what it is and use a non-erroring conversion method, especially when develeoping and it's not doing what you think....

                                        Here you are assuming its type is Double QMetaType::Double or QMetaType::LongLong But if it;s Null or Undefined you are not where you think you are...

                                        1 Reply Last reply
                                        1
                                        • Christian EhrlicherC Christian Ehrlicher

                                          Your code from the first post works fine for me. Make sure you're opening the correct json file. The conversion from/to QString is useless and wrong on non utf-8 systems though.

                                          int main(int argc, char *argv[])
                                          {
                                            QCoreApplication app(argc, argv);
                                          
                                            QByteArray content = R"(
                                          {
                                              "font": {
                                                  "family": "Calibri",
                                                  "pointSize": 11
                                              },
                                          
                                              "toolBarColor": {
                                                  "red": 156,
                                                  "green": 244,
                                                  "blue": 230
                                              },
                                          
                                              "oldSplashscreen": false,
                                              "darkTheme": false
                                          }
                                          )";
                                            QJsonDocument jsonDoc{QJsonDocument::fromJson(content)};
                                            QJsonObject jo{jsonDoc.object()};
                                            QJsonValue tempVal{jo.value("toolBarColor")};
                                            QJsonObject valueObj{tempVal.toObject()};
                                            QJsonValue redVal{valueObj.value("red")};
                                            QJsonValue greenVal{valueObj.value("green")};
                                            QJsonValue blueVal{valueObj.value("blue")};
                                            int red{redVal.toInt()};
                                            int green{greenVal.toInt()};
                                            int blue{blueVal.toInt()};
                                            qDebug() << red << green << blue;   // prints 156 244 230
                                            return 0;
                                          }
                                          
                                          
                                          H Offline
                                          H Offline
                                          HenkCoder
                                          wrote on last edited by
                                          #19

                                          Ooh now it works, I left a thing in the json file and forgot to save, I'm so dumb.
                                          Thanks to all who tried to help me tho!

                                          Christian EhrlicherC 1 Reply Last reply
                                          0
                                          • H HenkCoder

                                            Ooh now it works, I left a thing in the json file and forgot to save, I'm so dumb.
                                            Thanks to all who tried to help me tho!

                                            Christian EhrlicherC Offline
                                            Christian EhrlicherC Offline
                                            Christian Ehrlicher
                                            Lifetime Qt Champion
                                            wrote on last edited by
                                            #20

                                            @HenkCoder said in Can't convert from QJsonValue to int.:

                                            I'm so dumb.

                                            Please use the second parameter from QJsonDocument::fromJson() to get noticed about parser errors.

                                            ...

                                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                            Visit the Qt Academy at https://academy.qt.io/catalog

                                            H 1 Reply Last reply
                                            1

                                            • Login

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