Qt Websocket sending json
-
how can i send something like this
{"seg":{"fx":0}}
with websocketusing this
void Socket::sendBinary(const QString &bName, const QJsonValue bValue) { webSocket.sendTextMessage(QJsonDocument(QJsonObject{{bName, bValue}}).toJson(QJsonDocument::Compact)); }
-
And what's the acutal error? sendTextMessage() takes a QString but QJsonDocument::toJson() returns a (utf-8) encoded QByteArray so you should convert it to a QString first.
-
And what's the acutal error? sendTextMessage() takes a QString but QJsonDocument::toJson() returns a (utf-8) encoded QByteArray so you should convert it to a QString first.
@Christian-Ehrlicher well not any error
i know to send like this
connect(ui->pushButton, &QPushButton::clicked, [=]() { socket.sendBinary("doStuff", 0); });
with
void Socket::sendBinary(const QString &bName, const QJsonValue bValue) { webSocket.sendTextMessage(QJsonDocument(QJsonObject{{bName, bValue}}).toJson(QJsonDocument::Compact)); }
but how can i send
{"seg":{"fx":0}}
-
Hi,
Exactly as @Christian-Ehrlicher wrote: make a QString out of the QByteArray generated by toJson.
-
-
QString::fromUtf8 since you know that QJsonDocument::toJson returns an UTF-8 encoded document.
-
QString::fromUtf8 since you know that QJsonDocument::toJson returns an UTF-8 encoded document.
@SGaist am i doing something wrong?
void Socket::sendBinary(const QString &bName, const QJsonValue bValue) { QJsonObject obj; obj.insert("seq", "{'fx':10}"); QJsonDocument doc(obj); QString strJson(doc.toJson(QJsonDocument::Compact)); QString s2 = QString::fromUtf8(strJson.toUtf8().constData()); webSocket.sendTextMessage(s2); qDebug() << s2; }
-
@SGaist am i doing something wrong?
void Socket::sendBinary(const QString &bName, const QJsonValue bValue) { QJsonObject obj; obj.insert("seq", "{'fx':10}"); QJsonDocument doc(obj); QString strJson(doc.toJson(QJsonDocument::Compact)); QString s2 = QString::fromUtf8(strJson.toUtf8().constData()); webSocket.sendTextMessage(s2); qDebug() << s2; }
@Kris-Revi Why, what are you finding that is wrong?
-
@Kris-Revi Why, what are you finding that is wrong?
@JonB this is the ouput
"{\"seq\":\"{'fx':10}\"}"
but on the ESP32 that controlls the Led Strip the pattern does not change
im using the WLED software installed on the ESP32 and im trying to make a simple App in Qt to controll it via Websocket! i can connect and i can easily change brightness by sending
"bri", 0-255
but to change pattern i was told to send
{"seg":{"fx":0}}
-
@Kris-Revi Why, what are you finding that is wrong?
@JonB as you can see here
https://kno.wled.ge/interfaces/json-api/
"seg": [{ "start": 0, "stop": 20, "len": 20, "col": [ [255, 160, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0] ], "fx": 0, "sx": 127, "ix": 127, "pal": 0, "sel": true, "rev": false, "cln": -1 }]
-
@JonB this is the ouput
"{\"seq\":\"{'fx':10}\"}"
but on the ESP32 that controlls the Led Strip the pattern does not change
im using the WLED software installed on the ESP32 and im trying to make a simple App in Qt to controll it via Websocket! i can connect and i can easily change brightness by sending
"bri", 0-255
but to change pattern i was told to send
{"seg":{"fx":0}}
@Kris-Revi said in Qt Websocket sending json:
"{\"seq\":\"{'fx':10}\"}"
{"seg":{"fx":0}}
First, these are similar. The debugger (if that's what you're using to view output, you do not say) with show literal
"
s as\"
.Where I think you are showing a difference:
obj.insert("seq", "{'fx':10}");
You are inserting a single object with the literal string
"{'fx':10}"
as its value. But{"seg":{"fx":0}}
looks more like an outer object with object{"fx":0}
as its value, so you are not constructing your JSON correctly. But that has nothing to do with websocket or how you send the JSON. -
@Kris-Revi said in Qt Websocket sending json:
"{\"seq\":\"{'fx':10}\"}"
{"seg":{"fx":0}}
First, these are similar. The debugger (if that's what you're using to view output, you do not say) with show literal
"
s as\"
.Where I think you are showing a difference:
obj.insert("seq", "{'fx':10}");
You are inserting a single object with the literal string
"{'fx':10}"
as its value. But{"seg":{"fx":0}}
looks more like an outer object with object{"fx":0}
as its value, so you are not constructing your JSON correctly. But that has nothing to do with websocket or how you send the JSON.@JonB said in Qt Websocket sending json:
First, these are similar. The debugger (if that's what you're using to view output, you do not say)
yea the debugger (Application Output)...
how do i construct the json right with the outer object? i have only done the "easy" ("bri", 0-255)....
-
@JonB said in Qt Websocket sending json:
First, these are similar. The debugger (if that's what you're using to view output, you do not say)
yea the debugger (Application Output)...
how do i construct the json right with the outer object? i have only done the "easy" ("bri", 0-255)....
@Kris-Revi
I haven't looked up what the available functions are, but I imagine you need something more like:QJsonObject obj. obj2; obj2.insert("fx", 10); obj.insert("seq", obj2);
-
@SGaist am i doing something wrong?
void Socket::sendBinary(const QString &bName, const QJsonValue bValue) { QJsonObject obj; obj.insert("seq", "{'fx':10}"); QJsonDocument doc(obj); QString strJson(doc.toJson(QJsonDocument::Compact)); QString s2 = QString::fromUtf8(strJson.toUtf8().constData()); webSocket.sendTextMessage(s2); qDebug() << s2; }
@Kris-Revi said in Qt Websocket sending json:
@SGaist am i doing something wrong?
void Socket::sendBinary(const QString &bName, const QJsonValue bValue) { QJsonObject obj; obj.insert("seq", "{'fx':10}"); QJsonDocument doc(obj); QString strJson(doc.toJson(QJsonDocument::Compact)); QString s2 = QString::fromUtf8(strJson.toUtf8().constData()); webSocket.sendTextMessage(s2); qDebug() << s2; }
Two things in fact:
First, the single quotes around fx, it's invalid.
Second, the triple conversion. There's no need for s2.QByteArray jsonMessage(doc.toJson(QJsonDocument::Compact)); QString stringMessage = QString::fromUtf8(jsonMessage);