Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QJsonObject create nested Object



  • Hello,
    I want to generate a nested Json Object to transmit via QWebsocket.
    I could not find any documentation or example about how to create a nested object like:

    { 
        name: "name",
        type: "randomtype",
        children: [{ 
            name: "child1",
            type: "randomtype"},
            {name: "child2",
            type: "randomtype"}]
    }
    

    Is it possible to do this somehow in the normal QJsonObject notation like:

    QJsonObject json
        {
            {"type", "textMessage"},
            {"from", fromID},
            {"to",toID},
            {"time", QTime::currentTime().toString("hh:mm:ss")},
            {"name", fromName},
            {"line", message},
            {"mode", targetMode}
        };
    

  • Lifetime Qt Champion

    Hi and welcome to the forums.

    Do you mean a JSON array ? ( by nested)
    Like

      QJsonArray array;
        QJsonObject Obj {
            {"xxx", "yyyy"}
        };
        
        array.append( Obj );
    
        QJsonObject json {
            {"type", "textMessage"},
            {"Array", array}
        };
    


  • Partially yes,
    by nested i mean a json object inside another json object, in this case its an json array in an json object.
    Ill try what you posted, as it seems to be exactly what im looking for.


  • Lifetime Qt Champion

    @xeroe
    Hi
    Ok that is also possible

     QJsonObject json {
            {"type", "textMessage"},
            {"subobject", Obj}
        };
    

Log in to reply