I have a json message to send over a QTcpSocket. Before sending message, the Json message is formatted as below:
Case 1 :
//Heartbeat = QString("{\"messageType\":\"Heartbeat\", \"Thread_Name\":%1, \"Heartbeat\":%2}").arg(Thread_Name).arg(HbCount); //clients Heartbeat
Case 2:`
Heartbeat = QString("{ \
\"messageType\":\"Heartbeat\",\
\"Thread_Name\":\"%1\", \
\"Heartbeat\":%2 \
}").arg(Thread_Name).arg(HbCount); //clients Heartbeat
When QString is ready, it is transmitted over QTcpSocket as below:
_pSocket->write(Heartbeat.toLatin1());
_pSocket->flush();
_pSocket->waitForBytesWritten();
On receiving end, The socket able to receive data in both cases. But when received data is parsed using QJsonDocument, Case 1 fails giving empty json doc, where as Case 2 works giving the required JsonDoc.
Here is the receiving end code:
QByteArray Data = socket->readAll(); //read data from socket in to a QByteArray
QJsonDocument JsonDoc = QJsonDocument::fromJson(Data); //convert QByteArray to QJsonDocument
What is the difference between both cases? What is the best approach between the two?
In First Case, %1 is taking string argument. But Quotes are missing around it. When i added quotes as below, its working good.