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. Failing to extract value from JSON why?
Forum Updated to NodeBB v4.3 + New Features

Failing to extract value from JSON why?

Scheduled Pinned Locked Moved Unsolved General and Desktop
25 Posts 8 Posters 3.0k Views 4 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    I've been using the various JSON objects for a while now, but I I'm failing to see why I cannot extract the value from this:

    robjRequest is a reference to an instance of QJsonObject, in the debugger it shows:

    robjRequest:
    > [0] "commands".        <16 items>
    > [1] "file"             "~/XMLMPAM/config/test.dat"
    > [2] "module".          "mdFileIO"
    > [3] "position".        1
    

    I want to locate and extract position:

    QJsonObject::iterator itrFound = robjRequest.find(clsModFileIO::mscszPosition);
    
    if ( itrFound != robjRequest.end() ) {
        QString strLastKnown(itrFound.value().toString());
        qint64LastKnown = static_cast<qint64>(strLastKnown.toInt());
    }
    

    mscszPosition is defined as:

    const char clsModFileIO::mscszPosition[] = "position";
    

    I've stepped into the if condition and 'itrFound' is not equal to end so it has found the position, but the line to extract the value doesn't work, strLastKnown is "".

    Why could this be?

    Kind Regards,
    Sy

    KroMignonK 1 Reply Last reply
    0
    • SPlattenS SPlatten

      I've been using the various JSON objects for a while now, but I I'm failing to see why I cannot extract the value from this:

      robjRequest is a reference to an instance of QJsonObject, in the debugger it shows:

      robjRequest:
      > [0] "commands".        <16 items>
      > [1] "file"             "~/XMLMPAM/config/test.dat"
      > [2] "module".          "mdFileIO"
      > [3] "position".        1
      

      I want to locate and extract position:

      QJsonObject::iterator itrFound = robjRequest.find(clsModFileIO::mscszPosition);
      
      if ( itrFound != robjRequest.end() ) {
          QString strLastKnown(itrFound.value().toString());
          qint64LastKnown = static_cast<qint64>(strLastKnown.toInt());
      }
      

      mscszPosition is defined as:

      const char clsModFileIO::mscszPosition[] = "position";
      

      I've stepped into the if condition and 'itrFound' is not equal to end so it has found the position, but the line to extract the value doesn't work, strLastKnown is "".

      Why could this be?

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by KroMignon
      #2

      @SPlatten said in Failing to extract value from JSON why?:

      I want to locate and extract position:

      Why you don't use robjRequest.value(clsModFileIO::mscszPosition) ?

      EDIT: As far as I can see, position is not a string but an integer so toString() will not work and return an empty string! You should use toInt()

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      SPlattenS 1 Reply Last reply
      2
      • CharbyC Offline
        CharbyC Offline
        Charby
        wrote on last edited by
        #3

        QJsonValueRef::toString(const QString &defaultValue) returns the defaultValue when the QJsonValueRef is not a string (an empty string in your case).
        You might want to use isString() to confirm it is indeed a string.
        It seems "position" is actually an integer, so use toInt() to fetch it.

        SPlattenS 1 Reply Last reply
        3
        • KroMignonK KroMignon

          @SPlatten said in Failing to extract value from JSON why?:

          I want to locate and extract position:

          Why you don't use robjRequest.value(clsModFileIO::mscszPosition) ?

          EDIT: As far as I can see, position is not a string but an integer so toString() will not work and return an empty string! You should use toInt()

          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #4

          @KroMignon isn’t the purpose of toString to convert the value to a string?

          Kind Regards,
          Sy

          Christian EhrlicherC 1 Reply Last reply
          0
          • CharbyC Charby

            QJsonValueRef::toString(const QString &defaultValue) returns the defaultValue when the QJsonValueRef is not a string (an empty string in your case).
            You might want to use isString() to confirm it is indeed a string.
            It seems "position" is actually an integer, so use toInt() to fetch it.

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by
            #5

            @Charby , again I would expect toString to convert the type of the value to a string.

            Kind Regards,
            Sy

            JonBJ KroMignonK 2 Replies Last reply
            0
            • SPlattenS SPlatten

              @Charby , again I would expect toString to convert the type of the value to a string.

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

              @SPlatten
              No, why not always read the docs? QString QJsonValue::toString() const

              Converts the value to a QString and returns it.

              If type() is not String, a null QString will be returned.

              This function concerns the type of the value in the JSON, not what other type you might want to convert it to. Use QJsonValue::Type QJsonValue::type() const if you want to know the type of the value.

              I wouldn't recommend it, but QJsonValue::toVariant().toString() would do what you wanted.

              1 Reply Last reply
              3
              • SPlattenS SPlatten

                @Charby , again I would expect toString to convert the type of the value to a string.

                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by
                #7

                @SPlatten said in Failing to extract value from JSON why?:

                again I would expect toString to convert the type of the value to a string.

                You are wrong, the JSON you have create defines "position" as an integer { "position" : 1 } and not as a string { "position" : "1" }.
                QJsonObject.value() returns a QJsonValue and, as written in documentation:

                QString QJsonValue::toString() const
                Converts the value to a QString and returns it.
                If type() is not String, a null QString will be returned.

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                SPlattenS 1 Reply Last reply
                3
                • SPlattenS SPlatten

                  @KroMignon isn’t the purpose of toString to convert the value to a string?

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

                  @SPlatten said in Failing to extract value from JSON why?:

                  isn’t the purpose of toString to convert the value to a string?

                  The purpose of a function is to do what's documented... but I know reading documentation just takes too long...

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

                  SPlattenS fcarneyF 2 Replies Last reply
                  4
                  • Christian EhrlicherC Christian Ehrlicher

                    @SPlatten said in Failing to extract value from JSON why?:

                    isn’t the purpose of toString to convert the value to a string?

                    The purpose of a function is to do what's documented... but I know reading documentation just takes too long...

                    SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher oh... my sides... so funny!

                    Kind Regards,
                    Sy

                    1 Reply Last reply
                    0
                    • KroMignonK KroMignon

                      @SPlatten said in Failing to extract value from JSON why?:

                      again I would expect toString to convert the type of the value to a string.

                      You are wrong, the JSON you have create defines "position" as an integer { "position" : 1 } and not as a string { "position" : "1" }.
                      QJsonObject.value() returns a QJsonValue and, as written in documentation:

                      QString QJsonValue::toString() const
                      Converts the value to a QString and returns it.
                      If type() is not String, a null QString will be returned.

                      SPlattenS Offline
                      SPlattenS Offline
                      SPlatten
                      wrote on last edited by
                      #10

                      @KroMignon yes, i realise that but the name of the function is toString, so I expect the result to be whatever is on the left converted to a string.

                      Kind Regards,
                      Sy

                      KroMignonK JKSHJ 2 Replies Last reply
                      0
                      • SPlattenS SPlatten

                        @KroMignon yes, i realise that but the name of the function is toString, so I expect the result to be whatever is on the left converted to a string.

                        KroMignonK Offline
                        KroMignonK Offline
                        KroMignon
                        wrote on last edited by
                        #11

                        @SPlatten said in Failing to extract value from JSON why?:

                        yes, i realise that but the name of the function is toString, so I expect the result to be whatever is on the left converted to a string.

                        As always with Qt, there is a very good documentation.
                        Your first reflex should be to look there if the function does what you expect. It takes seconds and saves hours.

                        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          @SPlatten said in Failing to extract value from JSON why?:

                          isn’t the purpose of toString to convert the value to a string?

                          The purpose of a function is to do what's documented... but I know reading documentation just takes too long...

                          fcarneyF Offline
                          fcarneyF Offline
                          fcarney
                          wrote on last edited by
                          #12

                          @Christian-Ehrlicher said in Failing to extract value from JSON why?:

                          The purpose of a function is to do what's documented... but I know reading documentation just takes too long...

                          lol, TLDR RTFM!

                          C++ is a perfectly valid school of magic.

                          1 Reply Last reply
                          0
                          • SPlattenS SPlatten

                            @KroMignon yes, i realise that but the name of the function is toString, so I expect the result to be whatever is on the left converted to a string.

                            JKSHJ Offline
                            JKSHJ Offline
                            JKSH
                            Moderators
                            wrote on last edited by JKSH
                            #13

                            @SPlatten said in Failing to extract value from JSON why?:

                            the name of the function is toString, so I expect the result to be whatever is on the left converted to a string.

                            Following that logic, would you also always expect QJsonValue::toArray() to convert your value to an array, and QJsonValue::toObject() to convert your value to an object, and QJsonValue::toInt() to convert your value to an integer? Even if the value is not an array or an object or a number?

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

                            SPlattenS 1 Reply Last reply
                            4
                            • JKSHJ JKSH

                              @SPlatten said in Failing to extract value from JSON why?:

                              the name of the function is toString, so I expect the result to be whatever is on the left converted to a string.

                              Following that logic, would you also always expect QJsonValue::toArray() to convert your value to an array, and QJsonValue::toObject() to convert your value to an object, and QJsonValue::toInt() to convert your value to an integer? Even if the value is not an array or an object or a number?

                              SPlattenS Offline
                              SPlattenS Offline
                              SPlatten
                              wrote on last edited by
                              #14

                              @JKSH , of course, and if it couldn't then I would expect an error.

                              Kind Regards,
                              Sy

                              JonBJ KroMignonK 2 Replies Last reply
                              0
                              • SPlattenS SPlatten

                                @JKSH , of course, and if it couldn't then I would expect an error.

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

                                @SPlatten
                                I'm not criticizing you, just making an observation. It did report an error. But it reported an error by the toString() return a null QString(), which is an invalid string, for you to check for, rather than a "message"/"exception". Which is the Qt library way of returning errors.

                                SPlattenS 1 Reply Last reply
                                0
                                • SPlattenS SPlatten

                                  @JKSH , of course, and if it couldn't then I would expect an error.

                                  KroMignonK Offline
                                  KroMignonK Offline
                                  KroMignon
                                  wrote on last edited by
                                  #16

                                  @SPlatten said in Failing to extract value from JSON why?:

                                  if it couldn't then I would expect an error.

                                  Qt is avoiding exception usage, so what kind of error did you expect?
                                  And you should not expect to much but simply some time hit the F1 key on your keyboard to get the help for the function. All is documentation, you only have to read it.

                                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                  SPlattenS 1 Reply Last reply
                                  0
                                  • JonBJ JonB

                                    @SPlatten
                                    I'm not criticizing you, just making an observation. It did report an error. But it reported an error by the toString() return a null QString(), which is an invalid string, for you to check for, rather than a "message"/"exception". Which is the Qt library way of returning errors.

                                    SPlattenS Offline
                                    SPlattenS Offline
                                    SPlatten
                                    wrote on last edited by
                                    #17

                                    @JonB normally I always check the return of a function, I will revisit this code.

                                    Kind Regards,
                                    Sy

                                    JKSHJ 1 Reply Last reply
                                    0
                                    • KroMignonK KroMignon

                                      @SPlatten said in Failing to extract value from JSON why?:

                                      if it couldn't then I would expect an error.

                                      Qt is avoiding exception usage, so what kind of error did you expect?
                                      And you should not expect to much but simply some time hit the F1 key on your keyboard to get the help for the function. All is documentation, you only have to read it.

                                      SPlattenS Offline
                                      SPlattenS Offline
                                      SPlatten
                                      wrote on last edited by
                                      #18

                                      @KroMignon , Why is Qt avoid exception usage, if there is an error then there should be a mechanism for finding out what was wrong.

                                      Kind Regards,
                                      Sy

                                      JonBJ KroMignonK 2 Replies Last reply
                                      0
                                      • SPlattenS SPlatten

                                        @KroMignon , Why is Qt avoid exception usage, if there is an error then there should be a mechanism for finding out what was wrong.

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

                                        @SPlatten
                                        The answer is to this is historical/per language. C, and then C++, used function return results to indicate errors, not exceptions. (Exceptions are regarded as "expensive" in C++, but that's only one reason.) It's not always true now --- some C++ libraries do use exceptions --- but plenty stick with that approach. Qt is just following that pattern, which is not unreasonable for C++. Some other languages, e.g. C# or Python, tend to raise exceptions, as you prefer. That's just how it is.

                                        1 Reply Last reply
                                        2
                                        • SPlattenS SPlatten

                                          @KroMignon , Why is Qt avoid exception usage, if there is an error then there should be a mechanism for finding out what was wrong.

                                          KroMignonK Offline
                                          KroMignonK Offline
                                          KroMignon
                                          wrote on last edited by
                                          #20

                                          @SPlatten said in Failing to extract value from JSON why?:

                                          Why is Qt avoid exception usage

                                          Again, this is documented ;)
                                          https://wiki.qt.io/Coding_Conventions
                                          https://doc.qt.io/qt-5/exceptionsafety.html

                                          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                          1 Reply Last reply
                                          3

                                          • Login

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