HI @R-P-H ,
Besides converting the QByteArray to a QString, how would I extract the value if I added {} to the QByteArray ...
As long as the QByteArray contains a valid JSON value, you can either wrap it in [ and ] to make it a JSON array, or { "key": and } to make it a JSON object. If wrapping in an array, you would then fetch the first array item to get the original JSON value. Of if wrapping in an object, fetch the value corresponding to the key you used in the wrapper.
Here's a complete example:
const QByteArray json{ "\"My Value\" "};
qDebug().noquote() << "input " << json;
qDebug().noquote() << "parse-as-is " << QJsonDocument::fromJson(json); // Will fail.
const QJsonDocument arrayDoc = QJsonDocument::fromJson("[" + json + "]");
qDebug().noquote() << "wrapped-in-array " << arrayDoc;
qDebug().noquote() << "unwrapped-array " << arrayDoc.array().at(0);
const QJsonDocument objectDoc = QJsonDocument::fromJson("{ \"value\": " + json + "}");
qDebug().noquote() << "wrapped-in-object " << objectDoc;
qDebug().noquote() << "unwrapped-object " << objectDoc.object().value(QStringLiteral("value"));
Which outputs:
input "My Value"
parse-as-is QJsonDocument()
wrapped-in-array QJsonDocument(["My Value"])
unwrapped-array QJsonValue(string, My Value)
wrapped-in-object QJsonDocument({"value":"My Value"})
unwrapped-object QJsonValue(string, My Value)
Cheers.