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. convert QJsonValue to Int
QtWS25 Last Chance

convert QJsonValue to Int

Scheduled Pinned Locked Moved Solved General and Desktop
qjsonvalue
4 Posts 3 Posters 6.7k 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.
  • the_T Offline
    the_T Offline
    the_
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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_T 1 Reply Last reply
      2
      • JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by JKSH
        #3

        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
        2
        • SGaistS SGaist

          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.

          the_T Offline
          the_T Offline
          the_
          wrote on last edited by
          #4

          @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
          0

          • Login

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