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. QJsonDocument::toJson with subobjects

QJsonDocument::toJson with subobjects

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 717 Views 1 Watching
  • 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.
  • M Offline
    M Offline
    maisoui
    wrote on last edited by
    #1

    Hi,

    I'm trying to output json data by using QJsonDocument and QJsonObject, but I have a problem with subobjects. The output always contains null for subojects. Below a small example:

    QVariantHash sub;
    sub.insert("id", 2);
    sub.insert("name", "world");
    
    QVariantHash root;
    root.insert("id", 1);
    root.insert("name", "hello");
    root.insert("sub", QJsonObject::fromVariantHash(sub));
    
    QString output = QJsonDocument(QJsonObject::fromVariantHash(root)).toJson(QJsonDocument::Compact);
    

    Output = {"id":1,"name":"hello","sub":null}

    What I am missing?
    Best regards,
    Jonathan

    1 Reply Last reply
    0
    • the_T Offline
      the_T Offline
      the_
      wrote on last edited by
      #2

      Hi,

      you try to add a QJsonObject to root.
      If you try to add the QVariantHash to root you will get a valid json

      QVariantHash sub;
      sub.insert("id", 2);
      sub.insert("name", "world");
      
      QVariantHash root;
      root.insert("id", 1);
      root.insert("name", "hello");
      root.insert("sub", sub);
      
      QString output = QJsonDocument(QJsonObject::fromVariantHash(root)).toJson(QJsonDocument::Compact);
      
      qDebug() << output; // prints "{\"id\":1,\"name\":\"hello\",\"sub\":{\"id\":2,\"name\":\"world\"}}" 
      

      -- No support in PM --

      1 Reply Last reply
      1

      • Login

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