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.5k 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 Kris Revi
    24 Sept 2020, 12:29

    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'

    J Offline
    J Offline
    JonB
    wrote on 24 Sept 2020, 12:40 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 24 Sept 2020, 12:48 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

      J 1 Reply Last reply 24 Sept 2020, 12:57
      0
      • K Kris Revi
        24 Sept 2020, 12:48

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

        J Offline
        J Offline
        JonB
        wrote on 24 Sept 2020, 12:57 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 24 Sept 2020, 13:05 last edited by
          #7

          @JonB
          this is the Q_SIGNAL

          void onRequestedInfoReceived(QString message);
          

          this is the Q_SLOT

          void onRequestedInfoReceived(QString message);
          
          J 1 Reply Last reply 24 Sept 2020, 13:10
          0
          • K Kris Revi
            24 Sept 2020, 13:05

            @JonB
            this is the Q_SIGNAL

            void onRequestedInfoReceived(QString message);
            

            this is the Q_SLOT

            void onRequestedInfoReceived(QString message);
            
            J Offline
            J Offline
            JonB
            wrote on 24 Sept 2020, 13:10 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 24 Sept 2020, 13:36
            0
            • J JonB
              24 Sept 2020, 13:10

              @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 24 Sept 2020, 13:36 last edited by
              #9

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

              1 Reply Last reply
              0
              • J JonB
                24 Sept 2020, 13:10

                @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 24 Sept 2020, 14:25 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)
                
                J 1 Reply Last reply 24 Sept 2020, 14:49
                0
                • F Offline
                  F Offline
                  fcarney
                  wrote on 24 Sept 2020, 14:41 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
                    24 Sept 2020, 14:25

                    @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)
                    
                    J Offline
                    J Offline
                    JonB
                    wrote on 24 Sept 2020, 14:49 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 24 Sept 2020, 15:43 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!

                      J 1 Reply Last reply 24 Sept 2020, 16:36
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 24 Sept 2020, 15:48 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 24 Sept 2020, 16:15
                        0
                        • S SGaist
                          24 Sept 2020, 15:48

                          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 24 Sept 2020, 16:15 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
                            24 Sept 2020, 15:43

                            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!

                            J Offline
                            J Offline
                            JonB
                            wrote on 24 Sept 2020, 16:36 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 24 Sept 2020, 19:11
                            0
                            • B Offline
                              B Offline
                              Bonnie
                              wrote on 24 Sept 2020, 16:37 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
                              • J JonB
                                24 Sept 2020, 16:36

                                @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 24 Sept 2020, 19:11 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

                                13/18

                                24 Sept 2020, 15:43

                                • Login

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