-1

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?

ringul
  • 47
  • 7
  • what is the difference between case 1 and 2 exactly ?? they look identical ! – HazemGomaa May 31 '17 at 16:11
  • Thanks for your reply Gomaa. I resolved it. In the first case, %1 is taking string argument. But Quotes are missing around it. When i added quotes as below, its working good. `Heartbeat = QString("{\"messageType\":\"DetHeartbeat\", \"Thread_Name\":\"%1\", \"detHeartbeat\":%2}").arg(Thread_Name).arg(HbCount);` – ringul May 31 '17 at 18:26

1 Answers1

0

In First Case, %1 is taking string argument. But Quotes are missing around it. When i added quotes as below, its working good.

Heartbeat = QString("{\"messageType\":\"Heartbeat\", \"Thread_Name\":\"%1\", \"Heartbeat\":%2}").arg(Thread_Name).arg(HbCount);
ringul
  • 47
  • 7