Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Problem With JSON value type of data
Forum Updated to NodeBB v4.3 + New Features

Problem With JSON value type of data

Scheduled Pinned Locked Moved Solved Language Bindings
3 Posts 2 Posters 909 Views 2 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.
  • D Offline
    D Offline
    Daniziz
    wrote on 1 Oct 2018, 14:35 last edited by
    #1

    Hi all,

    I have a problem with my app when I try to get a JSON value in a string format.

    I POST a data to an API and the app gives me the JSON encapsulated in a "data" object:

            //Open a QJSonDocument to manage info from Asana
            jsonDoc = QJsonDocument::fromJson(response_data);
            //We point to "data" object where task_id is
            jsonObjectReply = jsonDoc.object().value("data").toObject();
    

    My problem here is that the field I need ("id") has this format:

           "id":844844570164347,
    

    Which is not an string (It doesent have ""). The thing is, when y try to debug the value, Qt gives me a double:

           qDebug() << jsonObjectReply["id"];
           //QJsonValue(double, 8.45045e+14)
    

    If I try to get a string (.toString()) or an int, it gives me an empty string or 0.

    Any idea why??

    Regards,

    Dani

    S 1 Reply Last reply 1 Oct 2018, 15:18
    0
    • D Daniziz
      1 Oct 2018, 14:35

      Hi all,

      I have a problem with my app when I try to get a JSON value in a string format.

      I POST a data to an API and the app gives me the JSON encapsulated in a "data" object:

              //Open a QJSonDocument to manage info from Asana
              jsonDoc = QJsonDocument::fromJson(response_data);
              //We point to "data" object where task_id is
              jsonObjectReply = jsonDoc.object().value("data").toObject();
      

      My problem here is that the field I need ("id") has this format:

             "id":844844570164347,
      

      Which is not an string (It doesent have ""). The thing is, when y try to debug the value, Qt gives me a double:

             qDebug() << jsonObjectReply["id"];
             //QJsonValue(double, 8.45045e+14)
      

      If I try to get a string (.toString()) or an int, it gives me an empty string or 0.

      Any idea why??

      Regards,

      Dani

      S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 1 Oct 2018, 15:18 last edited by
      #2

      @Daniziz said in Problem With JSON value type of data:

      Any idea why??

      As documentation states, Qt treats all JSON numbers as double, mostly because JSON specification does not clarify how implementations should handle numbers.

      If you have any influence on the API you are communicating with, please ask them to change their JSON output to contain a string ID.

      Or convert the double to int - but there you risk serious rounding errors.

      (Z(:^

      1 Reply Last reply
      5
      • D Offline
        D Offline
        Daniziz
        wrote on 2 Oct 2018, 06:11 last edited by
        #3

        Hi @sierdzio

        Thanks for the reply!

        The API already sends IDs in both formats: int (or double) and string.

        If that is the case I will use strings in my code.

        Thank you very much.

        Regards.

        1 Reply Last reply
        1

        1/3

        1 Oct 2018, 14:35

        • Login

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