Json sending and receiving and finding keys inside json
-
@Kris-Revi
[static]QJsonDocument QJsonDocument::fromJson(const QByteArray &json, QJsonParseError *error = nullptr)
accepts input as aQByteArray
:Parses json as a UTF-8 encoded JSON document,
I don't know where you got your
void MainWindow::onRequestedInfoReceived(QString message)
withQString
parameter from, data is sent around (serial ports, sockets) as bytes, so that's what it should be. If necessary, don't forget there isQByteArray QString::toUtf8() const
. -
@Kris-Revi
Fine.
And for the third time of suggesting/asking: how are you finding usingQByteArray QString::toUtf8() const
on it then works for making yourQString message
acceptable toQJsonDocument::fromJson()
with no error? I'm not going to repeat this further. -
@JonB did this
void MainWindow::onRequestedInfoReceived(QString message) { //qDebug() << "[SOCKET][INFO] Requested Info : " << message; QByteArray messageArray = message.toUtf8(); QJsonDocument doc = QJsonDocument::fromJson(messageArray); QJsonValue test = doc.object().value("001"); qDebug() << "Board Type : " << test; }
got
Board Type : QJsonValue(undefined)
-
Hi,
Side questions:
- Where are you getting that JSON data from ?
- Do you receive it directly a QString ?
- If not, why not change the slot signature to use a QByteArray, or even better use a const reference on a QByteArray.
-
@Kris-Revi said in Json sending and receiving and finding keys inside json:
the printing out part is realy not an issue :)
Well, yes/no, but You are wanting to get back the
doc2["300"] = DeviceType
/"300":"ESP32"
you put in, and you say you're not succedding withdoc.object().value("001")
, so you need to look at what the document you have parsed back has come out like.EDIT Hang on, aren't you looking for the wrong thing? When you showed the string received earlier it was:
[SOCKET][INFO] Requested Info : "{\"300\":\"ESP32\",\"301\":\"Matrix Display\",\"303\":\"Matrix\"}"
So why are you now asking for
doc.object().value("001");
?QJsonValue(undefined)
looks about the right for that!