c++qtqjson

How to convert QVariant to QJsonValue?


How to convert QVariant to QJsonValue? I know QVariant provide the toJsonValue function, but it did not perform as expected.

For example:

qDebug()<<QVariant(1.0).toJsonValue();
qDebug()<<QVariant("test").toJsonValue();

Both return:

QJsonValue(null)
QJsonValue(null)

Expect output:

QJsonValue(double, 1)
QJsonValue(string, "test")

Solution

  • You can use this static function too:

    QJsonValue::fromVariant( myVariant )
    

    Check this link for more info.