QJsonDocument doc = QJsonDocument::fromJson(jsonData); if(!doc.isNull()) { if(doc.isObject()) { QJsonObject obj = doc.object(); // Access object properties } else if(doc.isArray()) { QJsonArray arr = doc.array(); // Loop through array elements } }
QJsonObject obj; obj.insert("name", "John"); obj.insert("age", 25); QJsonDocument doc(obj); QByteArray jsonData = doc.toJson(QJsonDocument::Compact);This code shows how to create a QJsonObject and add properties to it. The object is then used to create a QJsonDocument, which is serialized to JSON data using the toJson() function. Package Library: QJsonDocument is part of the Qt framework, which is a cross-platform application development framework. Therefore, to use QJsonDocument, you need to install the Qt framework.