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

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.
  • T Offline
    T Offline
    the_
    wrote on 22 Jan 2016, 17:43 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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 22 Jan 2016, 22:23 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

      T 1 Reply Last reply 23 Jan 2016, 21:47
      2
      • J Offline
        J Offline
        JKSH
        Moderators
        wrote on 23 Jan 2016, 00:44 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
        • S SGaist
          22 Jan 2016, 22:23

          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.

          T Offline
          T Offline
          the_
          wrote on 23 Jan 2016, 21:47 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

          2/4

          22 Jan 2016, 22:23

          • Login

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