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.
Qt 6.11 is out! See what's new in the release blog

Can't convert from QJsonValue to int.

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 4 Posters 4.4k 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.
  • 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
                            • 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
                                #22
                                This post is deleted!
                                1 Reply Last reply
                                0
                                • JoeCFDJ Offline
                                  JoeCFDJ Offline
                                  JoeCFD
                                  wrote on last edited by
                                  #23

                                  Your data has problem. Use mine( my test code works) to see if your code works.

                                  {
                                      "font": {
                                          "family": "Calibri", 
                                          "pointSize": 11
                                      },
                                  
                                      "toolBarColor": {
                                          "red": 156,
                                          "green": 244,
                                          "blue": 230
                                      },
                                      
                                      "oldSplashscreen": false,
                                      "darkTheme": false
                                  }
                                  

                                  Are you working on Windows? the integer values may have some endings which can not be parsed properly by Qt code.
                                  I manually changed them. My test code works.
                                  In your output code add str = str.simplified() to get rid of them.

                                  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