[SOLVED] Send JSON data from client to server



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


  • Moderators

    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.



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


  • Moderators

    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?



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


  • Moderators

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



  • This solved my problem, thanks!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.