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
Highlighted
Developer
Posts: 142
Registered: ‎04-25-2009
My Device: Z30
My Carrier: AT&T
Accepted Solution

JSON data with two data maps into List

[ Edited ]

Hey all,

 

Been working well with JSON data and populating lists for awhile now, however im stuck now on a unique set of data.

 

The data structure is as follows:

 

[
     {
          data: {
               children: [ {
                    {}
                    {}
                    {}
               } ]
           }
     }

     {
          data: {
               children: [ {
                    {}
                    {}
                    {}
               } ]
           }
     }
]

 

 

 

Normally the data I deal with contains just one of these structures, and not 2 like in the data above. So the following could would work:

 

                                const QByteArray response(reply->readAll());
		ArrayDataModel *model = new ArrayDataModel();
		
		bb::data::JsonDataAccess jda;
		QVariantMap results = jda.loadFromBuffer(response).toMap();
		QVariantList children = results["data"].toMap()["children"].toList();

		model->append(children);
		mListView->setDataModel(model);

 

However, this is now giving me an empty list. So how can I restrict the above code to parse and insert only the 2nd set of JSON data into the list?

 

Please let me know if clarification is needed. Any help is appreciated. Thanks!

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: JSON data with two data maps into List

[ Edited ]

Hi,

 

It contains a list of maps. Have you tried something like the following:

 

bb::data::JsonDataAccess jda;
QVariantList results = jda.loadFromBuffer(response).toList();
if (results.size() >= 2)
{ QVariantMap secondSet = results.at(1).toMap(); // to get the second map QVariantList children = secondSet["data"].toMap()["children"].toList(); model->append(children);
}

 


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 142
Registered: ‎04-25-2009
My Device: Z30
My Carrier: AT&T

Re: JSON data with two data maps into List

Brilliant man! Thanks for such a quick reply, worked like a charm