Using QjsonDocument to parse api data in QT

254 views Asked by At

I have the below JSON which I am attempting to parse out.

{
    "btc_usd": {
        "high": 245.304,
        "low": 240.10001,
        "avg": 242.702005,
        "vol": 1406884.29972,
        "vol_cur": 5776.14075,
        "last": 242.9,
        "buy": 243.022,
        "sell": 242.9,
        "updated": 1435270412
    },
    "btc_rur": {
        "high": 13150,
        "low": 12912,
        "avg": 13031,
        "vol": 5397157.58979,
        "vol_cur": 413.89807,
        "last": 13139,
        "buy": 13139,
        "sell": 13071.00001,
        "updated": 1435270412
    }
}

This is how it comes formatted from the api in question.

However using the below example, I can not manage to pull out any of the data from the above json. I have attempted several ways to pull this data but have had no luck thus far.

QJsonDocument doc = QJsonDocument::fromBinaryData(reply->readAll());
QString rootObj = doc.object().value("").toString();
qDebug() << "results.count:" << rootObj.count();
1

There are 1 answers

0
phyatt On BEST ANSWER

Switch to using:

QJsonDocument::fromJson(reply->readAll()) instead of fromBinary and you should be good to go.

If you are still having trouble after that, pass in the optional QJsonParseError parameter into the static function and see what it is complaining about.

Hope that helps.