Creating QJsonDocument from json returns null

  • I'm trying to create a QJsonDocument (see minimal example below) and I'm getting all the time a void QJsonDocument. If I run the small program I get:
    is null: "no error occurred"

    I'm using Qt 5.9.3 (in archlinux). Any ideas?

    #include <QCoreApplication>
    #include <QDebug>
    #include <QJsonObject>
    #include <QJsonDocument>
    #include <QJsonParseError>
    int main(int argc, char *argv[])
        QCoreApplication a(argc, argv);
        QString json_str;
        QJsonDocument json_doc;
        QJsonParseError error_json;
        json_str = "{\"country\": \"Germany\", \"population\": 100 }";
        json_doc.fromJson(json_str.toUtf8(), &error_json);
        if (json_doc.isNull())
            qDebug() << "is null: " << error_json.errorString();
            qDebug() << "is not null";
        return a.exec();

  • fromJson is static and returns the parsed document

    json_doc=QJsonDocument::fromJson(json_str.toUtf8(), &error_json);

  • you're right @VRonin. It's working now. THanks a lot. Sorry for the noise.

Log in to reply

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