Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Creating QJsonDocument from json returns null
Forum Updated to NodeBB v4.3 + New Features

Creating QJsonDocument from json returns null

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rogorido
    wrote on last edited by
    #1

    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();
        else
            qDebug() << "is not null";
    
        return a.exec();
    }
    
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      fromJson is static and returns the parsed document http://doc.qt.io/qt-5/qjsondocument.html#fromJson

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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      5
      • R Offline
        R Offline
        rogorido
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved