2

How can I retrieve the integer value from the QJsonValue object? Suppose that I have the following JSON data:

    {
        "res": 1,
        "message": "Common error"
    }

I need to extract the "res" value from this data, so I've tried to use the following code:

QJsonDocument d = QJsonDocument::fromJson(some_json_data.toUtf8());
QJsonObject root_object = d.object();
QJsonValue res = root_object.value("res");

But I've found that QJsonValue class doesn't have a member function toInt or something like this (there's toDouble, toString, etc only). What can I do in such a situation? What is the best way to extract integer values via QjsonValue class?

3
  • 1
    This is discussed here: qt-project.org/forums/viewthread/24013 Mar 18, 2014 at 9:15
  • so it comes down to: use a different json library, or change qt :)
    – melak47
    Mar 18, 2014 at 9:19
  • If you want int, what's the problem with just using QJsonValue::toDouble() and casting to int? If you wanted a 64 bit integer, that would be a problem.
    – hyde
    Mar 18, 2014 at 13:17

3 Answers 3

4

(tl;dr: a one-liner solution at the end of answer.)

First a comprehensive way giving you full control. Below code assumes int is enough range for your integers, but could be expanded to work for most of the range of int64_t (but better test the boundary cases to get it fully correct):

QJsonValue res = root_object.value("res");

int result = 0;
double tmp = res.toDouble();
if (tmp >= std::numeric_limits<int>::min() && // value is not too small
    tmp <= std::numeric_limits<int>::max() && // value is not too big
    std::floor(tmp) == tmp // value does not have decimals, if it's not ok
   ) {
    result = std::floor(tmp); // let's be specific about rounding, if decimals are ok
} else {
    // error handling if you are not fine with 0 as default  value
}

A shorter way using QVariant, and as an example also getting result into a larger integer type, if you just want to let Qt do it's thing. I'm not sure how it it handles integer values which are too big for a double to handle accurately, so again if that is important, better test.

QJsonValue res = root_object.value("res");

QVariant tmp = res.toVariant();

bool ok = false;
qlonglong result = tmp.toLongLong(&ok);

if (!ok) {
    // error handling if you are not fine with 0 as default value
}

or same as error-ignoring one-liner, change integer type as appropriate:

qlonglong result = root_object.value("res").toVariant().toLongLong();
3

int QJsonValue::toInt(int defaultValue = 0) const

Converts the value to an int and returns it.

If type() is not Double or the value is not a whole number, the defaultValue will be returned.

QByteArray JSettings::read_file(QString path)
{
    QFile file;
    file.setFileName(path);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return QByteArray();

    QByteArray barray = file.readAll();
    file.close();
    return barray;
}

QVariantMap JSettings::to_map(QByteArray json)
{
    QJsonDocument jdoc = QJsonDocument::fromJson(json);
    QVariantMap vmap = jdoc.toVariant().toMap();
    // qDebug() << vmap["res"].toInt();
    return vmap;
}

QJsonObject JSettings::to_object(QByteArray json)
{
    QJsonDocument jdoc = QJsonDocument::fromJson(json);
    QJsonObject obj = jdoc.object();
    // qDebug() << obj["res"].toInt();
    return obj;
}

Qt JSON portal: http://doc.qt.io/qt-5/json.html

0
QJsonDocument d = QJsonDocument::fromJson(some_json_data.toUtf8());
QJsonObject root_object = d.object();
QJsonValue res = root_object.value("res");
int i = res.toString().toInt();

this is working, QString to Int

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.