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
SumiSadiq
Posts: 148
Registered: ‎04-08-2013
My Device: Blackberry 10

QVariant to QList problem

Hi,

I am trying to parse a json response. I am able to get the response and put it in a QVariant. But when I try to convert it to a QList, it doesnt happen. Please see my code 

QVariant jsonData = jda.loadFromBuffer(str);
qDebug() << jsonData;
QVariantList list = jsonData.toList(); // this is the problem
for (QList<QVariant>::iterator it = list.begin(); it != list.end(); it++)
	{
	 
	 QVariantMap map = it->toMap();
}

 

QString str = QString::fromUtf8(netReply->readAll());

Can someone suggest what could be gone wrong here. 

Developer
ekke
Posts: 1,571
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: QVariant to QList problem

is your data really a JSON Array ? or is the root a JSON Object ?

only array can be converted to list

if root is object then get the array inside this object and convert to list

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner http://vimeo.com/ekkescorner/videos
bb10-development: http://appbus.org Twitter: @ekkescorner
Developer
SumiSadiq
Posts: 148
Registered: ‎04-08-2013
My Device: Blackberry 10

Re: QVariant to QList problem

This is our JSON response. Not sure  wat is it.

{
    "response": {
        "code": 600,
        "message": "Success"
    },
    "payLoad": [
        {
            "sectionId": 1,
            "title": "Section",
            "titleArabic": "قسم",
            "titleColor": null,
            "shortDesc": null,
            "shortDescArabic": null,
            "shortDescColor": null,
            "imagePath": null,
            "position": null,
            "isActive": 1,
            "isVisible": 1,
            "isCmsApi": 1,
            "isTileView": 0,
            "isCmsSection": 0,
            "isDeletable": 0,
            "createdDate": 1371382620000,
            "updatedDate": 1371382620000
        }
]
}

 

Developer
ekke
Posts: 1,571
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: QVariant to QList problem

you shoul learn and read about JSON before trying to get access via Cascades / Qt ;-)

 

your root is an object {}

inside this you have another Object response {}

and an array payload []

 

so get your payload and convert to list and all will work

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner http://vimeo.com/ekkescorner/videos
bb10-development: http://appbus.org Twitter: @ekkescorner
Developer
SumiSadiq
Posts: 148
Registered: ‎04-08-2013
My Device: Blackberry 10

Re: QVariant to QList problem

Thank you.. :smileyhappy:

 

We tried this way.. 

QVariant jsonData = jda.loadFromBuffer(str);

	qDebug() << jsonData;
	  QVariantList list = jsonData.toList();
	  for (QList<QVariant>::iterator it = list.begin(); it != list.end(); it++)
	  {
	     QVariantMap map1 = it->toMap();
	  QVariant childJsonva = map1.value("payLoad");
	   QVariantList childList = childJsonva.toList();
        for (QList<QVariant>::iterator childIt = childList.begin(); childIt != childList.end(); childIt++)
	     	{
	     	QVariantMap map = childIt->toMap();
}
}

 it doesnt work either :smileysad: 

Developer
ekke
Posts: 1,571
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: QVariant to QList problem

as I wrote:

 

you don't have a List at Root

there's a JSON Object

get it as QVariantMap

then get the payload and convert to list

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner http://vimeo.com/ekkescorner/videos
bb10-development: http://appbus.org Twitter: @ekkescorner
Developer
CapQNX
Posts: 119
Registered: ‎01-11-2013
My Device: BlackBerry z10

Re: QVariant to QList problem

Hello friend,

    may be  below code will work:

   QList<QVariant> listVariant = QList<QVarint>();

listVariant.append(jsonData);

now you can use listVariant to get the data from it.

    Hope it helps.

Developer
CapQNX
Posts: 119
Registered: ‎01-11-2013
My Device: BlackBerry z10

Re: QVariant to QList problem

forgot to add just print it and get the data out of it.qDebug() <<"listvarint is:"<<listVariant;