#include#include // Create a JSON object QJsonObject obj; obj.insert("name", "John"); obj.insert("age", 30); QString jsonString = obj.toJson(QJsonDocument::Compact); // jsonString will contain {"name":"John","age":30}
#includeIn this example, we parse a JSON string using QJsonDocument and retrieve data from the resulting QJsonObject using the subscript operator []. Package/library: Qt (QtCore)#include #include // Parse a JSON string QString jsonString = "{\"name\":\"John\",\"age\":30}"; QJsonDocument doc = QJsonDocument::fromJson(jsonString.toUtf8()); QJsonObject obj = doc.object(); // Retrieve data QString name = obj["name"].toString(); // "John" int age = obj["age"].toInt(); // 30