c++qtqt5qjsonobjectqjsondocument

QJsonDocument to object is empty


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}'

Solution

  • 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