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
Posts: 126
Registered: ‎06-25-2013
My Device: Z10
My Carrier: none
Accepted Solution

How to extract data from json string where value in an array

Hi,

 

JSON String Structure

 

{"name" : "sanjeev" , "surname" : "kumar" , "address" : [{ "address1" : "value" ,  "place" : "value" }, { "address2" : "value" , "place" : "value" }] }

 


how to extract address value to list.

 

Developer
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone

Re: How to extract data from json string where value in an array

JsonDataAccess will give you a QVariantMap

and from this map you can get a QVariantList for 'address'

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

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development native Mobile Business Apps
BlackBerry 10 | Qt Mobile (Android, iOS)
Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org
mobile-development: http://appbus.org Twitter: @ekkescorner
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: How to extract data from json string where value in an array

Developer
Posts: 126
Registered: ‎06-25-2013
My Device: Z10
My Carrier: none

Re: How to extract data from json string where value in an array

i can extract value of name, surname by using JsonDataAccess

 

JsonDataAccess jda;

QVariant list = jda.loadFromBuffer(jsonString); 

QString name = list.toMap().values("name").value(0).toStrinng();

QString surname = list.toMap().values("surname").value(0).toStrinng();

 

 

Now my question is how to get the value of address in the jsonString which is an array .

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

Re: How to extract data from json string where value in an array

[ Edited ]

If you have control over the data format, it's better to rename address1 and address2 to have the same name, for example "address". Then it can be parsed like this (I've also simplified other code a bit):

 

JsonDataAccess jda;
QVariant v = jda.loadFromBuffer(jsonString);
QVariantMap m = v.toMap();
QString name = m["name"].toString();
QString surname = m["surname"].toString();
QVariantList addresses = m["address"].toList();
foreach (QVariant addr, addresses)
{
QVariantMap addrMap = addr.toMap(); QString addressValue = addrMap["address"].toString(); QString place = addrMap["place"].toString(); }

 


Andrey Fidrya, @zmeyc on twitter