Results 1 to 5 of 5

Thread: qjson int64 range

  1. #1
    Join Date
    Dec 2015
    Location
    Rome
    Posts
    16
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default qjson int64 range

    Hello all,
    I am running ina trouble, I need to work with int64 but I did not found a solution except to use they like strings.
    In the following program the statement 'Doc.object()["ID"].toDouble();' return very close value but not exact, in my environment.
    There is some way to extract the entire number without use the string form ("IDs":"635913899220803700") ?




    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QJsonDocument>
    3. #include <QJsonParseError>
    4. #include <QJsonObject>
    5.  
    6. int main(int argc, char *argv[]){
    7. QCoreApplication a(argc, argv);
    8.  
    9. QString message=R"RR({"ID":635913899220803700,"IDs":"635913899220803700"})RR";
    10. QJsonParseError err;
    11.  
    12. QJsonDocument Doc=QJsonDocument::fromJson(message.toUtf8(),&err);
    13.  
    14. unsigned long long ia=Doc.object()["ID"].toDouble();
    15. unsigned long long ib=_atoi64(Doc.object()["IDs"].toString().toUtf8().data());
    16. _ASSERT(ia==ib);
    17.  
    18. return a.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 


    Cheers,
    Giorgio
    Last edited by oop; 18th February 2016 at 16:29. Reason: updated contents

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qjson int64 range

    Call toString() on the QJsonValue and then toLongLong() on the QString.

    Cheers,
    _

  3. #3
    Join Date
    Dec 2015
    Location
    Rome
    Posts
    16
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: qjson int64 range

    Hi,
    I tried similar solution but seems that the resulting QString is empty.


    unsigned long long ib=Doc.object()["ID"].toString().toLongLong(); // ib is 0


    Or also:
    QString sval=Doc.object()["ID"].toString();
    unsigned long long ib=sval.toLongLong();


    There is some think that escape me?

    Giorgio

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qjson int64 range

    So it seems that the value returned from object()["ID"] is a JavaScript Number, which is a double precision floating point value.

    Since you need integer, you'll have to make the value a string. then you can extract the string and parse as a C++ int64.

    Cheers,
    _

  5. #5
    Join Date
    Dec 2015
    Location
    Rome
    Posts
    16
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: qjson int64 range

    Yes, I seen that I need to use the string form (IDs as above code) in order to extract the int64 number.
    I found other info around internet about JsonValue.


    Thank you.
    Giorgio

Similar Threads

  1. A problem about QJson
    By greatcl in forum Qt Programming
    Replies: 3
    Last Post: 14th May 2013, 10:20
  2. QJson
    By WetCode in forum Qt Programming
    Replies: 4
    Last Post: 20th February 2013, 22:54
  3. Qjson
    By Girija in forum Qt Programming
    Replies: 8
    Last Post: 17th September 2010, 14:19
  4. How to install Qjson in mac
    By Girija in forum Newbie
    Replies: 21
    Last Post: 17th September 2010, 09:55
  5. How to add a plugin? (QJson)
    By Thomas Wrobel in forum Newbie
    Replies: 5
    Last Post: 3rd December 2009, 21:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.