Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
noahhuang
Posts: 100
Registered: ‎10-01-2012
My Device: BB10 Dev Alpha
Accepted Solution

Does anyone know how to parse a JsonArray inside a Json Object using QJson?

I tried using QJson to parse and get the JsonArray inside the Json. but not succeed. Has anyone one hase experience or exmaple codes can share?

 

Big thanks.

Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900

Re: Does anyone know how to parse a JsonArray inside a Json Object using QJson?

You should use RIM's JsonDataAccess, it uses QVariants and you can get arrays with json.value("listKey").toList()

Like all of my posts
Developer
noahhuang
Posts: 100
Registered: ‎10-01-2012
My Device: BB10 Dev Alpha

Re: Does anyone know how to parse a JsonArray inside a Json Object using QJson?

This is the data I try to parse.

I need get the Owner's id and related fields. And I tried use JDataAccess API.  After parse the data, I get empty string.

 

 

RESPONSE: {
    responseCode: SUCCESS | FAIL | EXCEPTION ,
    debugMessage: "some addition details when responseCode is FAIL | EXCEPTION ",
    data: [
        {
            'Owner': {
                'province': null,
                'city': null,
                'active': 1,
                'id': 3348
            },
            'unitName': '12345',
            'active': 1,
            'id': 3130
        },
        {
            'Owner': {
                'province': null,
                'city': null,
                'active': 1,
                'id': 3348
            },
            'unitName': '23456',
            'active': 1,
            'id': 3131
        }
    ]
}

Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900

Re: Does anyone know how to parse a JsonArray inside a Json Object using QJson?

JsonDataAccess json = JsonDataAccess::loadFromBuffer(qstring);

QVariantList data = json.value("data").toList();

foreach(QVariant v, data) {

v.toMap().value("Owner").toMap().value("id").toInt()

}

Like all of my posts
Developer
noahhuang
Posts: 100
Registered: ‎10-01-2012
My Device: BB10 Dev Alpha

Re: Does anyone know how to parse a JsonArray inside a Json Object using QJson?

Thanks this can work:

//

QJson::

Parserdriver;

 

QFile file(QDir::homePath() + "/" + user->m_vehicleFile);

 

boolok;

 

if (file.exists()) {

 

QVariantMap rootMap = driver.parse(&file, &ok).toMap();

 

if(!ok) {

 

qFatal("An error occurred during parsing");

exit(1);

}

QVariantList data = rootMap["data"].toList();

foreach(

QVariantv, data){

 

qDebug()<< v.toMap().value("unitName").toString();

}

//