简体   繁体   中英

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 为空

How can I resolve this?

Data:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM