QJsonDocument to object is empty

755 Views Asked by At

I'm trying to parse a simple JSON data in Qt5.

Code looks like this :

    ...
    socket->readDatagram(Buffer.data(),Buffer.size(),&sender,&senderPort);

    QJsonParseError jsonError;
    QJsonDocument dataJson = QJsonDocument::fromJson(Buffer.data(),&jsonError);
    if (jsonError.error != QJsonParseError::NoError){
        qDebug() << jsonError.errorString();
    }
    QJsonObject map = dataJson.object();
    //map["x"].toDouble()

But for some reason my map is empty, here is a debugging snap : QJsonDocument to object is empty

How can I resolve this ?

Data :

'{\"x\":1,\"y\":2,\"z\":3}'
1

There are 1 best solutions below

8
On BEST ANSWER

Assuming that you're reading correctly, you should test with a command like this:

echo -n \{\"x\":1,\"y\":2,\"\z\":3\} > /dev/udp/127.0.0.1/8080

So, get rid of single quotes and escape curly brackets.

Even better: put your json data in a myfile file and use cat myfile > /dev/udp/127.0.0.1/8080