Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved convert QJsonValue to Int

    General and Desktop
    qjsonvalue
    3
    4
    5715
    Loading More Posts
    • 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.
    • the_
      the_ last edited by

      I got a (for me) strange behaviour with Json Objects.
      When i try to read a json value and convert it to Int with toInt() i always get the default value (0 if not specified with toInt() method) instead of the read value.
      here is a small example:

      //assume jobj is a QJsonObject andcontains {"ID":"680","auditarea":"General","auditdate":"2015-12-23"}
      qDebug() << jobj.value("ID").toInt(); //always returns default value
      qDebug() << jobj.value("ID").toString().toInt(); // returns 680
      

      Is there something I do wrong here?

      Thanks for some hints.

      -- No support in PM --

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        ID doesn't contain a number but a number in a string hence the need for both conversions. If you wanted to send a number for ID, then you JSON is wrong.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        the_ 1 Reply Last reply Reply Quote 2
        • JKSH
          JKSH Moderators last edited by JKSH

          To clarify @SGaist's point, see the difference between the 2 objects below:

              "ObjWithString": {
                  "ID": "680" 
              },
              "ObjWithInt": {
                  "ID": 680
              }
          
          

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

          1 Reply Last reply Reply Quote 2
          • the_
            the_ @SGaist last edited by

            @SGaist, @JKSH

            Thanks for clarification. As i get this JSON from a webservice (not coded by me) i will have to use the "hard" way of JSON Value -> String -> Int to get an integer value

            -- No support in PM --

            1 Reply Last reply Reply Quote 0
            • First post
              Last post