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
Regular Contributor
Posts: 83
Registered: ‎07-19-2012
My Device: Bold 9790, Dev Alpha A
My Carrier: Koodo

Accessing different hierarchal level in a JsonDataAccess

Hey everyone,

I'm trying to think of the best way to access different levels of a JsonDataAccess and then display that information in either a ListView or labels/textfields. So far I have no problem accessing the first level because a lot of that is covered in the sample apps and tutorials. My issue is finding out a way to get even farther down in the hierachy (see below)

 

For instance, lets say I have the following JSON:

 

{
     "currentPage":1,
     "numberOfPages":1,
     "data":[
          {
               "id":"id1",
               "name":[
                    {
                         "firstName":"firstName1",
                         "lastName":"lastName1",
                    }],
               "website":"website1",
               "status":"status1"
          },
          {
               "id":"id2",
               "name":[
                    {
                         "firstName":"firstName2",
                         "lastName":"lastName2",
                    }],
               "website":"website2",
               "status":"status2"
          },          
          {
               "id":"id3",
               "name":[
                    {
                         "firstName":"firstName3",
                         "lastName":"lastName3",
                    }],
               "website":"website3",
               "status":"status3"
          }
       ],
     "status":"success"
}

 I can extract "data" by using the following code

		//New GroupDataModel where the parsed json data will be stored
		GroupDataModel* dm = new GroupDataModel();

		//load json data
		bb::data::JsonDataAccess ja;
		QVariant jsonva = ja.loadFromBuffer(strReply);

		//find "data" in QVariant and put into a QVariantList
		QVariantList list = jsonva.toMap().find("data")->toList();

		//Iterate through "data" and put into data model
		for (QList<QVariant>::iterator it = list.begin(); it != list.end(); it++) {
			QVariantMap itMap = it->toMap();
			dm->insert(itMap);
		}

 

Next it is easy to display "id", "website", and "status" in a listView but what if I also wanted to display "firstName" and "lastName". Would I have to find the "name" list in the map and then iterate again (similar to the first iteration) to put into a map and then into a dataModel? Or is there a more simple way of just accessing "names" from the original JSON Variant?

Developer
Posts: 849
Registered: ‎03-03-2011
My Device: Z10

Re: Accessing different hierarchal level in a JsonDataAccess

In my opinion you need to create a new map with all linear (non-tree) values, e.g.:
"id":"id3",
"firstName":"firstName3",
"lastName":"lastName3",
"website":"website3",
"status":"status3"

So you will have to go on level deeper to do that.

--------------------------------------------------------------------------------------
Leader of Ljubljana BlackBerry Developer Group
BlackBerry Certified Builder for Native Application Development
Retired
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: Accessing different hierarchal level in a JsonDataAccess

You have 4 choices that I see:

 

1. Read the JSON and feed data into a GroupDataModel or QListDataModel

2. Read the JSON, write a JSON to be consumed by a GroupDataModel or QListDataModel

3. Change the format of your data file

4. Use your own data model.

 

Most people will select 1 I expect.

 

In some cases I could see 2 being an acceptable choice, but I would likely select 1 over 2 myself.

 

Choice 3 might be appropriate if your data format is somewhat arbitrary.  If it's just a matter of flattening first and last name I would consider it.  Long-term, forcing your data representation into the form consumed by the generic data models might not be a good idea if your data is hierarchical by nature, as this forces a limitation on the data representation due to the way you want to display it today.

 

I would tend to the last choice myself because I personally find it easier than (1).  In the more complicated choices, I would read the JSON into my own data model, and use a DataModel subclass to present this to the UI.  You can read up on this in the KB article: http://supportforums.blackberry.com/t5/Cascades-Development-Knowledge/Using-your-own-DataModel/ta-p/...

 

Stuart

 

Regular Contributor
Posts: 83
Registered: ‎07-19-2012
My Device: Bold 9790, Dev Alpha A
My Carrier: Koodo

Re: Accessing different hierarchal level in a JsonDataAccess

Hmmm...how would I "go a level deeper"?

I cannot use my own data model becuase this is the reply from a REST call.
Retired
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: Accessing different hierarchal level in a JsonDataAccess

[ Edited ]

I'm not sure I understand the connection between the DataModel choice and restful.

And I'm not sure I understand what you mean by "go a level deeper".

 

The DataModel subclass merely presents some data to the ListView.  You can use a supplied class, or you can roll your own.  Your own can do all the work (see http://supportforums.blackberry.com/t5/Cascades-Development-Knowledge/Using-your-own-DataModel/ta-p/... or it can sit between the ListView and some other data model (see http://supportforums.blackberry.com/t5/Cascades-Development-Knowledge/Using-a-Custom-DataModel-to-Fi...  It can also inherit from a built in instead of sit as an intermediary if that is more convenient.   In many of these cases, the DataModel is playing the role of a controller in MVC parlance.

 

Can you reword your questions?

 

Stuart

Developer
Posts: 849
Registered: ‎03-03-2011
My Device: Z10

Re: Accessing different hierarchal level in a JsonDataAccess

Use C++ that goes N levels deeper and than put maps of the subnode into data model.
--------------------------------------------------------------------------------------
Leader of Ljubljana BlackBerry Developer Group
BlackBerry Certified Builder for Native Application Development
Regular Contributor
Posts: 83
Registered: ‎07-19-2012
My Device: Bold 9790, Dev Alpha A
My Carrier: Koodo

Re: Accessing different hierarchal level in a JsonDataAccess

I hope this helps clarify my situation. From looking at the JSON model I originally posted I want to be able to display "firstName", "lastName", "Website" and "status" in a listView populated from a data model. Then, once an option from the ListView is selected a form will be created where I will populate either Labels or TextFields with the same data.

My main issue arises from not being able to populate the ListView with "firstName" or "lastName" whereas "Website" and "status" are easy using ListItemData.website or ListItemData.status.

Now my question is, how would one go about putting "firstName" and "lastName" into a dataModel in C++ so that I can populate a ListView?
Retired
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: Accessing different hierarchal level in a JsonDataAccess

Is this not already answered above?

I'm not sure where you are stuck.

 

Stuart

Regular Contributor
Posts: 83
Registered: ‎07-19-2012
My Device: Bold 9790, Dev Alpha A
My Carrier: Koodo

Re: Accessing different hierarchal level in a JsonDataAccess

I'm stuck wondering if a GroupDataModel is the right choice given that I have 3 levels of hierarchal data. I was reading the documentation for GroupDataModel (here) and I think that GroupDataModel will only accept two levels of data heirarchy. If that is the case, would an XmlDataModel be a better choice for my case? (Note: I'm making REST calls that can reply in JSON or XML and I chose JSON because it was default).

Highlighted
Regular Contributor
Posts: 83
Registered: ‎07-19-2012
My Device: Bold 9790, Dev Alpha A
My Carrier: Koodo

Re: Accessing different hierarchal level in a JsonDataAccess

Scratch that.

 

My REST call returns XML with elements instead of attributes. Unfortunately XmlDataModel only supports attributes so I dont want to go down the road of reformatting XML elements into XML attributes quite yet.

 

I think at this point it would be better to change the format of my data to fit a two level model like Soaman suggested in the first reply on this thread.

 

Soaman, do you have any code that would accomplish this? I find it really difficult to learn how QVariant, QVariantMap, QVariantList, etc work because there isn't much documentation on it. Thanks!