Problem With JSON value type of data



  • 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


  • Moderators

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



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.