c++qtqt5qjsonqjsonobject

QJsonValue gets empty


When I make request to the time API to get the Moscow time, I'm getting empty int in the variable. Here's the code:

QNetworkReply* reply = manager->get(QNetworkRequest(QUrl("https://yandex.com/time/sync.json?geo=213")));
QEventLoop loop;
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();

QByteArray data = reply->readAll();
QJsonDocument document = QJsonDocument::fromJson(data);
QJsonObject root = document.object();
qDebug() << data;
qDebug() << root.value("time").toInt();

Here's the JSON:

{"time":1519489796585,"clocks":{"213":{"id":213,"name":"Moscow","offset":10800000,"offsetString":"UTC+3:00","showSunriseSunset":true,"sunrise":"07:33","sunset":"17:53","isNight":true,"skyColor":"#3f68b2","weather":{"temp":-10,"icon":"ovc","link":"/moscow"},"parents":[{"id":225,"name":"Russia"}]}}}

Solution

  • If the number is revised: 1519490351963, this exceeds the greater value of an integer: 2147483647, so there is no conversion to whole, a possible solution is to convert it to double:

    // qDebug() << root["time"].toDouble();
    qDebug() << root.value("time").toDouble();
    

    Another possible solution is to convert it to QVariant and then use toLongLong() to use qlonglong which is a type of integer that supports more bits.

    //qDebug() << root["time"].toVariant().toLongLong();
    qDebug() << root.value("time").toVariant().toLongLong();