QJson Parsing help request



  • Hey, guys!

    I have the following json data:

    {
    "IssueInstant": "2015-03-19T14:54:41.563054Z",
    "NotAfter": "2015-03-19T22:54:41.563054Z",
    "id": "12345",
    "cat": {
    "test": [{
    "a": "aData",
    "b": "bData",
    "c": "cData",
    "d": "dData",
    "e": "eData"
    }]
    }
    }

    I want access to the a-e values. Right now I'm doing:

    QJsonDocument jsonResponse = QJsonDocument::fromJson(ba);
    QJsonObject jsonObj = jsonResponse.object();
    id = jsonObj["id"].toString();
    

    This works fine to get upper level values, but not the sub ones. Any ideas? Thanks in advance!



  • @metaDom said:

    Hey, guys!

    I have the following json data:

    {
    "IssueInstant": "2015-03-19T14:54:41.563054Z",
    "NotAfter": "2015-03-19T22:54:41.563054Z",
    "id": "12345",
    "cat": {
    "test": [{
    "a": "aData",
    "b": "bData",
    "c": "cData",
    "d": "dData",
    "e": "eData"
    }]
    }
    }

    I want access to the a-e values. Right now I'm doing:

    QJsonDocument jsonResponse = QJsonDocument::fromJson(ba);
    QJsonObject jsonObj = jsonResponse.object();
    id = jsonObj["id"].toString();
    

    This works fine to get upper level values, but not the sub ones. Any ideas? Thanks in advance!

    It looks that cat is an Object with a member test. Test itself is an Array which contains one Object. This Object has the members a to e. So you have to convert cat to an jsonobject, grab the test member, convert this to an jsonarray, grab the only and first entry, convert this to jsonobject and access the members a to e ...


  • Moderators

    Hi,

    @metaDom said:

    QJsonDocument jsonResponse = QJsonDocument::fromJson(ba);
    QJsonObject jsonObj = jsonResponse.object();
    id = jsonObj["id"].toString();
    

    This works fine to get upper level values, but not the sub ones. Any ideas? Thanks in advance!

    You get the sub ones in the same way. "cat" is an object, so you convert the value to an object:

    QJsonObject catObj = jsonObj["cat"].toObject();
    

    "cat" is an object that contains "test", which is an array that contains 1 object that contains your 5 string elements.

    This page might be useful: http://doc.qt.io/qt-5/qjsonvalue.html


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.