How to Create dynamically JSON object in QT ?
-
wrote on 15 Feb 2018, 07:47 last edited by Mayank Thakor
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.
-
-
wrote on 15 Feb 2018, 09:03 last edited by
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 propertiesso How i will create JSON messages and how i will add different property type in JSON ??
-
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.
-
wrote on 15 Feb 2018, 12:06 last edited by Mayank Thakor
THANK YOU
i want like this and i understand thisQJsonObject obj;
obj.insert("someKey", "someValue");
obj.insert("someOtherKey", 123);
obj.insert("array", QStringList() {"a", "b"});
QJsonDocument doc(obj).toJson(); // <- this is your JSON stringbut
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 ?
-
wrote on 15 Feb 2018, 17:19 last edited by ODБOï
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
-
wrote on 16 Feb 2018, 05:56 last edited by
-
wrote on 16 Feb 2018, 13:35 last edited by
@Mayank-Thakor if your issue is finally solved, please don't forget to mark your post as such! thank you.
1/8