Format JSON
-
Hello, I get this JSON from one webservice:
"{ \"response_code\": 1, \"sha256\": \"c8e61eacbbh31ae51e50a0083e5429bb6bea0f4d822c2793cd58cbf17e3638c1\", \"resource\": \"c8e61egcbbd31ae51e50a0093e5429bb6bea0fed822c2794cd58rbf17e3638c1\", \"scan_id\": \"c8e61earbd31ae51e50a0093e5429bb6bee0f4d822c2794cd58cbf1fe3638c1e1531522520\" }"
But if I do:
QJsonDocument document = QJsonDocument::fromJson(reply); QJsonObject rootObj = document.object(); qDebug() << rootObj.value(QString("scan_id"));
I will get:
QJsonValue(undefined)
So I think it is because that JSON is bad formated (it contains backslashes), is possible to format it into correct form?
-
@Mr-Gisa Replacing backslashes of whole reply, can be dangerous because it can corrupt data inside (if message contain backshlash).
@kshegunov Problem is that I cannot get value of node:
qDebug() << rootObj.value(QString("scan_id"));
It says:
QJsonValue(undefined)
@Taz742 Tried and didn't help.
-
@mrjj Yea it removed backslahes, but why this doesn't work?
QJsonDocument document = QJsonDocument::fromBinaryData(reply); QJsonObject rootObj = document.object(); qDebug().noquote() << rootObj.value(QString("scan_id"));
I still get:
QJsonValue(undefined)
-
Hi
The returned QJsonValue is QJsonValue::Undefined if the key does not exist.
so it sounds like it dont have "scan_id" key
Try to dump what rootObj contains and see. what is in there.
also try
https://jsonlint.com/
to see if likes the json.
also reading the docs it seems
QJsonDocument::fromBinaryData expects data to be a blob and i think you have
just a string value so try with
QJsonDocument::fromJson instead. ( or again. sorry)also check QJsonDocument if it parses correctly.
You have no error checking so it could be anything!
we are flying blind here :)
something like. (there will be dragons. not compiled)
QJsonParseError error;
QJsonDocument document = QJsonDocument::fromJson(reply, &error );
if (document.isNull() {
qDebug() << "Error: " << error.errorString() << error.offset;
} -
@t0msk said in Format JSON:
Hello, I get this JSON from one webservice:
What a network traffic capture shows about data actually received?