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

Json, QMap & QList

I've been trying to figure out how to insert data correctly into a data model using QMap and QList.

Here is the data I have thats been loaded from a Json file:

[
    {
        "firstName" : "Mike",
        "lastName" : "Chepesky",
        "contactInfo":[
        	{
        		"email":"test@test.com",
        		"phone":"1-234-567-8910"
        	}
        ],
        "employeeNumber" : 01840192
    },
    {
        "firstName" : "Westlee",
        "lastName" : "Barichak",
        "contactInfo":[
        	{
        		"email":"test2@test2.com",
        		"phone":"1-234-567-8910"
        	}
        ],
        "employeeNumber" : 47901927
    },
    {
        "firstName" : "Ian",
        "lastName" : "Dundas",
        "contactInfo":[
        	{
        		"email":"test3@test3.com",
        		"phone":"1-234-567-8910"
        	}
        ],
        "employeeNumber" : 29472012
    }
]

 And I would like to put all of that data into a single hierarchy and then into a GroupDataModel. Sort of like this:

[
    {
        "firstName" : "Mike",
        "lastName" : "Chepesky",
        "email":"test@test.com",
        "phone":"1-234-567-8910",
        "employeeNumber" : 01840192
    },
    {
        "firstName" : "Westlee",
        "lastName" : "Barichak",
        "email":"test2@test2.com",
        "phone":"1-234-567-8910",
        "employeeNumber" : 47901927
    },
    {
        "firstName" : "Ian",
        "lastName" : "Dundas",
        "email":"test3@test3.com",
        "phone":"1-234-567-8910",
        "employeeNumber" : 29472012
    }
]

 so far I have the following code which will get "firstName", "lastName" and "employeeNumber" into the GroupDataModel but I can't seem to figure out how to get "email" and "phone" in there.

 

    ListView *listView = root->findChild<ListView*>("qmlListView");

    GroupDataModel *model = new GroupDataModel(QStringList() << "firstName" << "lastName");
JsonDataAccess jda; QVariant jsonva = jda.load(QDir::currentPath() + "/app/native/assets/employees.json"); QVariantList list = jsonva.toList(); for (QList<QVariant>::iterator it = list.begin(); it != list.end(); it++) { QVariantMap map = it->toMap(); model->insert(map); } listView->setDataModel(model);

 

 

Please use plain text.
Developer
greenback
Posts: 496
Registered: ‎10-17-2010
My Device: BlackBerry Z10, DAC

Re: Json, QMap & QList

BUMP

Please use plain text.
Regular Contributor
CrozyBB
Posts: 83
Registered: ‎07-19-2012
My Device: Bold 9790, Dev Alpha A
My Carrier: Koodo

Re: Json, QMap & QList

Found a solution. The data hierarchy is preserved even as key:value pairs are set into QMap and QList. Therefor, all that needs to be done is to find the child array and throw that into a list. Once that's done you can extract the list values and unite those with the original map as it goes through an iterator. Check out the code below and if anyone has any questions contact me.

    JsonDataAccess jda;

    //load JSON to a QVariant
    QVariant jsonva = jda.load(QDir::currentPath() + "/app/native/assets/employees.json");

    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);

 

Please use plain text.