Json sending and receiving and finding keys inside json
-
wrote on 24 Sept 2020, 12:10 last edited by aha_1980
so here im sending a response when "001" (Request Info) is sent to the ESP32
DynamicJsonDocument doc2(256); // Init Dynamic Json Document String jsonData; // Make Json Variable To Store Outgoing Json if (doc["001"] && doc["001"] == "yes") { // Request Info Serial.println("Requested Info"); doc2["300"] = DeviceType; // Device Type doc2["301"] = DeviceName; // Device Name doc2["303"] = DeviceLEDType; // Device LED Type serializeJson(doc2, jsonData); // Serialize Json webSocket.sendTXT(num, jsonData); // Send Json Data Via Socket Serial.println(jsonData); }
Printing out jsonData it looks like this
{"300":"ESP32","301":"Matrix Display","303":"Matrix"}
perfect! now on the other end
void MainWindow::onRequestedInfoReceived(QString message) { qDebug() << "[SOCKET][INFO] Requested Info : " << message; }
Printing out message looks this
[SOCKET][INFO] Requested Info : "{\"300\":\"ESP32\",\"301\":\"Matrix Display\",\"303\":\"Matrix\"}"
why the \ because of QString?
-
so here im sending a response when "001" (Request Info) is sent to the ESP32
DynamicJsonDocument doc2(256); // Init Dynamic Json Document String jsonData; // Make Json Variable To Store Outgoing Json if (doc["001"] && doc["001"] == "yes") { // Request Info Serial.println("Requested Info"); doc2["300"] = DeviceType; // Device Type doc2["301"] = DeviceName; // Device Name doc2["303"] = DeviceLEDType; // Device LED Type serializeJson(doc2, jsonData); // Serialize Json webSocket.sendTXT(num, jsonData); // Send Json Data Via Socket Serial.println(jsonData); }
Printing out jsonData it looks like this
{"300":"ESP32","301":"Matrix Display","303":"Matrix"}
perfect! now on the other end
void MainWindow::onRequestedInfoReceived(QString message) { qDebug() << "[SOCKET][INFO] Requested Info : " << message; }
Printing out message looks this
[SOCKET][INFO] Requested Info : "{\"300\":\"ESP32\",\"301\":\"Matrix Display\",\"303\":\"Matrix\"}"
why the \ because of QString?
wrote on 24 Sept 2020, 12:19 last edited by@Kris-Revi
What is your question? Yes,qDebug()
displaying a string shows embedded double-quote characters as\"
. -
wrote on 24 Sept 2020, 12:29 last edited by
my question is does this affect when i try to
QJsonDocument doc = QJsonDocument::fromJson(message); QJsonValue test = doc.object().value("001");
also this gives me
no viable conversion from 'QString' to 'const QByteArray'
-
my question is does this affect when i try to
QJsonDocument doc = QJsonDocument::fromJson(message); QJsonValue test = doc.object().value("001");
also this gives me
no viable conversion from 'QString' to 'const QByteArray'
wrote on 24 Sept 2020, 12:40 last edited by@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
. -
wrote on 24 Sept 2020, 12:48 last edited by
i can only set QString
void MainWindow::onRequestedInfoReceived(QString message)
it wont allow me to set QByteArray or bytes or anything realy :S -
i can only set QString
void MainWindow::onRequestedInfoReceived(QString message)
it wont allow me to set QByteArray or bytes or anything realy :Swrote on 24 Sept 2020, 12:57 last edited by@Kris-Revi
I assume that method is a slot, and since you don't show the signal connection I cannot comment.So did you try the
QByteArray QString::toUtf8() const
? -
@JonB
this is theQ_SIGNAL
void onRequestedInfoReceived(QString message);
this is the
Q_SLOT
void onRequestedInfoReceived(QString message);
wrote on 24 Sept 2020, 13:10 last edited by@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. -
@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. -
@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.wrote on 24 Sept 2020, 14:25 last edited by@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)
-
wrote on 24 Sept 2020, 14:41 last edited by
What does
qDebug().noquote() << message;
do?
It might remove the quoting altogether and make it more readable. -
@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)
wrote on 24 Sept 2020, 14:49 last edited by JonB@Kris-Revi
Print out things likeQJsonDocument::isNull()
,QJsonDocument::isObject()
,QJsonDocument::object()
. What about looking atdoc.object().keys()
to see what is actually in there? -
wrote on 24 Sept 2020, 15:43 last edited by
the printing out part is realy not an issue :) i just noticed that there was added
\
in the print out and i thought that that would affect the way im searching for keys! -
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.
-
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.
-
the printing out part is realy not an issue :) i just noticed that there was added
\
in the print out and i thought that that would affect the way im searching for keys!wrote on 24 Sept 2020, 16:36 last edited by JonB@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! -
wrote on 24 Sept 2020, 16:37 last edited by
I'm guessing that OP get his data from QWebSocket.
So if he connectsQWebSocket::textMessageReceived
signal, then he'll get directly a QString. -
@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!
6/18