Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt Websocket sending json
Forum Updated to NodeBB v4.3 + New Features

Qt Websocket sending json

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 4 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kris Revi
    wrote on 10 Aug 2022, 15:49 last edited by
    #1

    how can i send something like this {"seg":{"fx":0}} with websocket

    using this

    void Socket::sendBinary(const QString &bName, const QJsonValue bValue)
    {
        webSocket.sendTextMessage(QJsonDocument(QJsonObject{{bName, bValue}}).toJson(QJsonDocument::Compact));
    }
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 10 Aug 2022, 16:43 last edited by
      #2

      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.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      K 1 Reply Last reply 10 Aug 2022, 18:30
      2
      • C Christian Ehrlicher
        10 Aug 2022, 16:43

        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.

        K Offline
        K Offline
        Kris Revi
        wrote on 10 Aug 2022, 18:30 last edited by
        #3

        @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}}

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 10 Aug 2022, 18:38 last edited by
          #4

          Hi,

          Exactly as @Christian-Ehrlicher wrote: make a QString out of the QByteArray generated by toJson.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Kris Revi
            wrote on 11 Aug 2022, 16:36 last edited by
            #5

            like this?

            void Socket::sendBinary(const QString &bName, const QJsonValue bValue)
            {    
                QJsonObject obj;
                obj.insert("seq", "{'fx':0}");
                QJsonDocument doc(obj);
                QString strJson(doc.toJson(QJsonDocument::Compact));
            
                webSocket.sendTextMessage(strJson);
            }
            
            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 11 Aug 2022, 17:14 last edited by
              #6

              QString::fromUtf8 since you know that QJsonDocument::toJson returns an UTF-8 encoded document.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              K 1 Reply Last reply 14 Aug 2022, 07:52
              0
              • S SGaist
                11 Aug 2022, 17:14

                QString::fromUtf8 since you know that QJsonDocument::toJson returns an UTF-8 encoded document.

                K Offline
                K Offline
                Kris Revi
                wrote on 14 Aug 2022, 07:52 last edited by Kris Revi
                #7

                @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;
                
                }
                
                J S 2 Replies Last reply 14 Aug 2022, 08:51
                0
                • K Kris Revi
                  14 Aug 2022, 07:52

                  @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;
                  
                  }
                  
                  J Offline
                  J Offline
                  JonB
                  wrote on 14 Aug 2022, 08:51 last edited by
                  #8

                  @Kris-Revi Why, what are you finding that is wrong?

                  K 2 Replies Last reply 14 Aug 2022, 08:56
                  0
                  • J JonB
                    14 Aug 2022, 08:51

                    @Kris-Revi Why, what are you finding that is wrong?

                    K Offline
                    K Offline
                    Kris Revi
                    wrote on 14 Aug 2022, 08:56 last edited by
                    #9

                    @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}}

                    J 1 Reply Last reply 14 Aug 2022, 09:24
                    0
                    • J JonB
                      14 Aug 2022, 08:51

                      @Kris-Revi Why, what are you finding that is wrong?

                      K Offline
                      K Offline
                      Kris Revi
                      wrote on 14 Aug 2022, 09:17 last edited by
                      #10

                      @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
                          }]
                      
                      1 Reply Last reply
                      0
                      • K Kris Revi
                        14 Aug 2022, 08:56

                        @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}}

                        J Offline
                        J Offline
                        JonB
                        wrote on 14 Aug 2022, 09:24 last edited by
                        #11

                        @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.

                        K 1 Reply Last reply 14 Aug 2022, 09:30
                        0
                        • J JonB
                          14 Aug 2022, 09:24

                          @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.

                          K Offline
                          K Offline
                          Kris Revi
                          wrote on 14 Aug 2022, 09:30 last edited by
                          #12

                          @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)....

                          J 1 Reply Last reply 14 Aug 2022, 10:34
                          0
                          • K Kris Revi
                            14 Aug 2022, 09:30

                            @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)....

                            J Offline
                            J Offline
                            JonB
                            wrote on 14 Aug 2022, 10:34 last edited by
                            #13

                            @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);
                            
                            1 Reply Last reply
                            1
                            • K Kris Revi
                              14 Aug 2022, 07:52

                              @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;
                              
                              }
                              
                              S Offline
                              S Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on 14 Aug 2022, 18:25 last edited by
                              #14

                              @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);
                              

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              1

                              1/14

                              10 Aug 2022, 15:49

                              • Login

                              • Login or register to search.
                              1 out of 14
                              • First post
                                1/14
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved