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
robtraf
Posts: 48
Registered: ‎03-27-2011
My Device: BlackBerry Z10

Re: How to iterate thru Json parsed file and retrieve key:value pairs.

Thank you. Sorry should be "condition" not "current_condition", typing error!

No I did not write the Json structure, came off the internet, although your not wrong in saying its really bad!!! 

I might try and find a better Json structure to use. Again thank you for all your help and I will keep you posted on my outcome.

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

Re: How to iterate thru Json parsed file and retrieve key:value pairs.

Keep in mind that the hierarchy structure of the standard GroupDataModel only supports 2 levels, a root and a singe sub root. If you want to be able to load the JSON data from your code into a data model without modifying it at all then you will need to create your own group data model (here and here).

 

You can also cimply modify the structure of the JSON using an iterator loop and put all of the values into a single map. Check out this code that I came up with and see if that helps you at all.

Developer
robtraf
Posts: 48
Registered: ‎03-27-2011
My Device: BlackBerry Z10

Re: How to iterate thru Json parsed file and retrieve key:value pairs.

Thank you. I have tried to make the Json file a little bit simpler.

I have the following now:-

 

{"success":true,"error":null,"response":{"id":"YYY","loc":{"long":144.81666666667,"lat":-37.666666666667},"place":{"name":"airport","state":"","country":"au"},"profile":{"tz":"Australia\/Melbourne"},"obTimestamp":1350007200,"obDateTime":"2012-10-12T13:00:00+11:00","ob":{"timestamp":1350007200,"dateTimeISO":"2012-10-12T13:00:00+11:00","tempC":13,"tempF":55,"humidity":55,"pressureMB":1021}}}

 

All object and no arrays?

The code you pointed out is what i require, only i need to retrieve for QMap and QVariant?

I have successfully parsed the data from Json, just require the appropriate code to insert into groupdatamodel.

I dont require all the data, for example if I only wanted to populate the model with the values from obDateTime, tempC and humidity how would i iterate thru the Json data?

 

Below is the code from your example: what would i need to change so I can retrieve my data?

   QVariantList list = jsonva.toList();

    for (QList<QVariant>::iterator it = list.begin(); it != list.end(); it++)
	{
    	//Key-value pairs from list item put into a map
    	QVariantMap map = it->toMap();
    	
    	//search key-value pairs for specific key of the array, in this case "contactInfo"
    	QVariant childJsonva = map.value("contactInfo");
    	
    	//Take the key-value pairs from the variant above and put into QList 
    	QVariantList childList = childJsonva.toList();

    	//iterate through the child list and put each key-value pair into a map
    	for (QList<QVariant>::iterator childIt = childList.begin(); childIt != childList.end(); childIt++)
    	{
    		QVariantMap childMap = childIt->toMap();
    		
    		//combine child map with the original map
    		map.unite(childMap);
    	}
    	
		model->insert(map);
	}

    // Set the data model for the list view
    listView->setDataModel(model);
Developer
svmrajesh
Posts: 164
Registered: ‎07-23-2013
My Device: Blackberry z10

Re: How to iterate thru Json parsed file and retrieve key:value pairs.

Try this link....

 

http://supportforums.blackberry.com/t5/Native-Development/webservice-help-json/m-p/2569941#M46270

___________________________________________________________________________________________

*************Plese *********LIKE**** & ****ACCEPT as Solution*** If you got suggestion for your Query.

@RAJESH SVM ----- Blackberry Mobile App Developer, Blaze, India.