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

How to Create dynamically JSON object in QT ?



  • I need to insert the value in that JSON object.

    give some reference link or documents so i will create dynamically JSON object in my qt creator.


  • Moderators



  • correct
    But i need to create multiple JSON message dynamically
    and from that multiple JSON messages
    i need to filler that JSON message base on their property type and properties

    so How i will create JSON messages and how i will add different property type in JSON ??


  • Moderators

    I'm sorry but I don't understand what you want to achieve. Creating a simple JSON message is something akin to:

    QJsonObject obj;
    obj.insert("someKey", "someValue");
    obj.insert("someOtherKey", 123);
    obj.insert("array", QStringList() {"a", "b"});
    QJsonDocument doc(obj).toJson(); // <- this is your JSON string
    

    To create multiple messages, just repeat the process... I don't see a problem here. But then again, as said, I don't understand what your question really is about.



  • THANK YOU
    i want like this and i understand this

    QJsonObject obj;
    obj.insert("someKey", "someValue");
    obj.insert("someOtherKey", 123);
    obj.insert("array", QStringList() {"a", "b"});
    QJsonDocument doc(obj).toJson(); // <- this is your JSON string

    but

    I want to create JSON object dynamically (with new ) and i want to give TYPE and other property.

    for example
    one jason message it's TYPE is "jason_type_1" and it has 2 property like "jason_1_property_1" and "jason_property_2".

    second jason message it's TYPE is "jason_type_2" and it has 3 property like "jason_2_property_1" , "jason_2_property_2" and "jason_3_property_3".

    so base on the TYPE "jason_type_1" or "jason_type_2" i can filter the message and
    then i read property like
    QString read_string = (jsonObj["body"].toObject()["jason_1_property_1"]).toString();

    so can you please give example how i will create this in QT ?



  • hi @Mayank-Thakor,

    Im not sure but, would it be possible to insert your 'TYPE ' and 'property ' directly in the json obj itself ?

    QJsonObject obj;
    obj.insert("TYPE", "jason_type_1"); // type
    obj.insert("prop1", jason_1_property_1); // property 1
    obj.insert("prop1", jason_1_property_2); // property 2
    obj.insert("array", QStringList() {"a", "b"});
    QJsonDocument doc(obj).toJson(); // <- this is your JSON string
    

    Edit : also look this http://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html



  • @LeLev said in How to Create dynamically JSON object in QT ?:

    QStringList

    Okay got it

    Thank you !



  • @Mayank-Thakor if your issue is finally solved, please don't forget to mark your post as such! thank you.


Log in to reply