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. [SOLVED] Send JSON data from client to server
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Send JSON data from client to server

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 10.3k 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.
  • F Offline
    F Offline
    flopoe
    wrote on 28 Jan 2015, 14:14 last edited by
    #1

    I am trying to send some JSON data from client to server. I am reading information from database and store that in a JSON object. Here is the code:
    @QString columnName = qry.record().fieldName(i);

    QJsonValue jVal(qry.value(i).toString());
    jObj->insert(columnName, jVal);@

    I am pretty sure that jObj stores the correct information. All that I need is sending jObj to server. This is how I write to the tcpSocket:
    @QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_0);

    out << quint16(0);
    QString jSonToSend = ????? ; // Don't know what to do here
    out << jSonToSend;

    out.device()->seek(0);
    out << (quint16)(block.size() - sizeof(quint16));

    tcpSocket->write(block);
    @

    What I think is if I can convert my jObj to Qstring I can send it to server as QString and parse it in my server program to store in another database. Also I've tried converting jObj to QByteArray and writing it to tcpSocket but it sends nothing("") to the server.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 28 Jan 2015, 14:20 last edited by
      #2

      use "QJsonDocument::​toJson()":http://doc.qt.io/qt-5/qjsondocument.html#toJson

      So you also should rather store your JSON data in a QJsonDocument than in a QJsonValue object.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • F Offline
        F Offline
        flopoe
        wrote on 28 Jan 2015, 14:41 last edited by
        #3

        Should I set my jObj to QJsonDocument(jDoc) object with " QJsonDocument::setObject(const QJsonObject & object)":http://doc-snapshot.qt-project.org/qt5-5.4/qjsondocument.html#setObject then write the OJsonDocument object to tcpSocket like @tcpSocket->write(jDoc.toJson());@

        Since I write my data as QByteArray to tcpSocket I commented out this part:
        @QByteArray block;
        QDataStream out(&block, QIODevice::WriteOnly);
        out.setVersion(QDataStream::Qt_4_0);

        out << quint16(0);
        QString jSonToSend = ????? ; // Don't know what to do here
        out << jSonToSend;

        out.device()->seek(0);
        out << (quint16)(block.size() - sizeof(quint16));
        @ and directly wrote the data as @tcpSocket->write(jDoc->toJson());@

        However this doesn't send any data to server.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 28 Jan 2015, 15:18 last edited by
          #4

          i can entirely work with QJsonDocument.
          So when you receive your string you can do for example QJsonDocument::fromJson(myJsonString)

          Did you check if toJson() actually returned something useful?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • F Offline
            F Offline
            flopoe
            wrote on 28 Jan 2015, 15:26 last edited by
            #5

            Yes it returns something useful but after I write it to tcpSocket it doesn't send anything to server. Isn't there any conversion which converts whole JSON formatted text to QString?

            1 Reply Last reply
            0
            • R Offline
              R Offline
              raven-worx
              Moderators
              wrote on 28 Jan 2015, 15:36 last edited by
              #6

              [quote author="flopoe" date="1422456119"]Isn't there any conversion which converts whole JSON formatted text to QString?[/quote]
              The method i mention does that already. The returned QByteArray is a UTF-8 encoded string.

              So if you are sure that you don't write 'nothing' on the socket i would say there is something wrong with your socket implementation.

              Or are you sure the socket connection is fine at this point?

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • F Offline
                F Offline
                flopoe
                wrote on 28 Jan 2015, 20:55 last edited by
                #7

                This solved my problem, thanks!

                1 Reply Last reply
                0

                1/7

                28 Jan 2015, 14:14

                • Login

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