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. Json sending and receiving and finding keys inside json
Forum Updated to NodeBB v4.3 + New Features

Json sending and receiving and finding keys inside json

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 5 Posters 1.3k Views 2 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 last edited by
    #3

    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'

    JonBJ 1 Reply Last reply
    0
    • K Kris Revi

      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'

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #4

      @Kris-Revi
      [static]QJsonDocument QJsonDocument::fromJson(const QByteArray &json, QJsonParseError *error = nullptr) accepts input as a QByteArray:

      Parses json as a UTF-8 encoded JSON document,

      I don't know where you got your void MainWindow::onRequestedInfoReceived(QString message) with QString parameter from, data is sent around (serial ports, sockets) as bytes, so that's what it should be. If necessary, don't forget there is QByteArray QString::toUtf8() const.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Kris Revi
        wrote on last edited by
        #5

        i can only set QString void MainWindow::onRequestedInfoReceived(QString message) it wont allow me to set QByteArray or bytes or anything realy :S

        JonBJ 1 Reply Last reply
        0
        • K Kris Revi

          i can only set QString void MainWindow::onRequestedInfoReceived(QString message) it wont allow me to set QByteArray or bytes or anything realy :S

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #6

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

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Kris Revi
            wrote on last edited by
            #7

            @JonB
            this is the Q_SIGNAL

            void onRequestedInfoReceived(QString message);
            

            this is the Q_SLOT

            void onRequestedInfoReceived(QString message);
            
            JonBJ 1 Reply Last reply
            0
            • K Kris Revi

              @JonB
              this is the Q_SIGNAL

              void onRequestedInfoReceived(QString message);
              

              this is the Q_SLOT

              void onRequestedInfoReceived(QString message);
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #8

              @Kris-Revi
              Fine.
              And for the third time of suggesting/asking: how are you finding using QByteArray QString::toUtf8() const on it then works for making your QString message acceptable to QJsonDocument::fromJson() with no error? I'm not going to repeat this further.

              K 2 Replies Last reply
              0
              • JonBJ JonB

                @Kris-Revi
                Fine.
                And for the third time of suggesting/asking: how are you finding using QByteArray QString::toUtf8() const on it then works for making your QString message acceptable to QJsonDocument::fromJson() with no error? I'm not going to repeat this further.

                K Offline
                K Offline
                Kris Revi
                wrote on last edited by
                #9

                @JonB wait... QByteArray QString::toUtf8() const ? :S

                1 Reply Last reply
                0
                • JonBJ JonB

                  @Kris-Revi
                  Fine.
                  And for the third time of suggesting/asking: how are you finding using QByteArray QString::toUtf8() const on it then works for making your QString message acceptable to QJsonDocument::fromJson() with no error? I'm not going to repeat this further.

                  K Offline
                  K Offline
                  Kris Revi
                  wrote on last edited by
                  #10

                  @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)
                  
                  JonBJ 1 Reply Last reply
                  0
                  • fcarneyF Offline
                    fcarneyF Offline
                    fcarney
                    wrote on last edited by
                    #11

                    What does

                    qDebug().noquote() << message; 
                    

                    do?
                    It might remove the quoting altogether and make it more readable.

                    C++ is a perfectly valid school of magic.

                    1 Reply Last reply
                    0
                    • K Kris Revi

                      @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)
                      
                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #12

                      @Kris-Revi
                      Print out things like QJsonDocument::isNull(), QJsonDocument::isObject(), QJsonDocument::object(). What about looking at doc.object().keys() to see what is actually in there?

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        Kris Revi
                        wrote on last edited by
                        #13

                        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!

                        JonBJ 1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #14

                          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.

                          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
                          0
                          • SGaistS SGaist

                            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.
                            K Offline
                            K Offline
                            Kris Revi
                            wrote on last edited by
                            #15

                            @SGaist scroll to the top and you will see where i get that json from and the answer to the QString question :)

                            1 Reply Last reply
                            0
                            • K Kris Revi

                              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!

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by JonB
                              #16

                              @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 with doc.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!

                              K 1 Reply Last reply
                              0
                              • B Offline
                                B Offline
                                Bonnie
                                wrote on last edited by
                                #17

                                I'm guessing that OP get his data from QWebSocket.
                                So if he connects QWebSocket::textMessageReceived signal, then he'll get directly a QString.

                                1 Reply Last reply
                                0
                                • JonBJ 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 with doc.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!

                                  K Offline
                                  K Offline
                                  Kris Revi
                                  wrote on last edited by
                                  #18

                                  @JonB you are actualy correct! xD im looking for "303" NOT the "001" that is "Request Info"

                                  1 Reply Last reply
                                  0

                                  • Login

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