How to extract non {"key":"value"} JSON ?
-
Hi, I am receiving a JSON response using the following code:
QByteArray json = reply->readAll(); QJsonDocument data = QJsonDocument::fromJson(json);
This works perfectly for JSON such as:
{"mykey": "myvalue"}
which I can then access using
data["mykey"].toString()
.However, when I get a JSON response such as
json = ""My value""
,data
isNULL
.How can I extract the JSON text correctly?
-
HI @R-P-H ,
Besides converting the
QByteArray
to aQString
, 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.
-
Since
{"myvalue²}
is no valid json - never.
See https://www.json.org/json-en.html -
HI @R-P-H ,
Besides converting the
QByteArray
to aQString
, 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.
-