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
Philipk
Posts: 676
Registered: ‎05-06-2012
My Device: Dev Alpha & PlayBook
My Carrier: wifi
Accepted Solution

C++ parse JSON

Given the following JSON

 

[
{ "id":1, "firstname": "Mike", "lastname": "Chepesky", "title": "Sr. Editor",
  "image": "images/data/mike_chepesky.png", "active": true, "gender": "m" },
{ "id":2, "firstname": "Westlee", "lastname": "Barichak", "title": "Talent Scout",
  "image": "images/data/westlee_barichak.png", "active": true, "gender": "m" },
...
]

What would be the code to extract and set first and last names as QStrings?

 

I'm assuming JsonDataAccess will be used but I'm not sure how to use it. 

_________________________________________________________
co-founder of Diaree | BB Dev for PictureThis
Please use plain text.
Developer
Philipk
Posts: 676
Registered: ‎05-06-2012
My Device: Dev Alpha & PlayBook
My Carrier: wifi

Re: C++ parse JSON

I think I'm loading the json correctly with the following 

 

		JsonDataAccess jda;
		QVariant obj = jda.load(message);

 Assuming that the above is correct(which required that jda.load accepts a string). I'm now trying to figure out how to extract a single value e.g. 

 

obj.name

 

_________________________________________________________
co-founder of Diaree | BB Dev for PictureThis
Please use plain text.
Developer
Zmey
Posts: 1,510
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: C++ parse JSON

[ Edited ]

Hi!

 

This thread contains some sample code:

http://supportforums.blackberry.com/t5/Native-Development/How-to-extract-data-from-json-string-where...

 

There's one catch: JsonDataAccess will return QVariantList if there are multiple top-level items, but QVariantMap otherwise. If you're working with lists, this needs to be checked explicitly. upd: nevermind, I confused JsonDataAccess with XmlDataAccess where this problem is present. :smileyhappy:

 

To save it back to disk:

 

jda.save(anyMapOrListWhichCanBeWrappedInQVariant, filename);

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.