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
Highlighted
Developer
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Parse JSON data

[ Edited ]

Hi, in my app I receive a reply from server which is JSON data. The reply is contained in the following form: QNetworkReply* reply. In my code, if I call:

void JSONSample::requestFinished(QNetworkReply* reply)
{
...
mFile->write(reply->readAll()); mFile->flush(); mFile->close(); qDebug() << "\n File opened";
...

 I manage to successfully write the JSON contents that I received on the server to a file.

But, if I just try to read the contents of reply into a buffer I get an empty string, since an empty string is printed by qDebug() as below.

 

...
const QByteArray response(reply->readAll()); qDebug() << response;

what can I be doing wrong???

 

ps. And also one question. As I mentioned there is JSON data contained in the "reply". Of course I know

nothing about its structure in advance (i.e., whether it starts with an array or just elements -- I just want to be able to read the whole JSON file regardless of what structure it has. How is it possible to do this?). this sample here: https://developer.blackberry.com/cascades/documentation/device_platform/data_access/working_with_jso... assumes the first element in the json file is an array -- but as I mentioned I don't know in advance how my JSON structure looks like.

 

pps. For instance, I have code:

... 
const QByteArray response(reply->readAll()); qDebug() << response; JsonDataAccess jda; QVariantMap results = jda.loadFromBuffer(response).toMap();

 Is this the right way to do it ???? How to traverse the whole "results" now ????

 

Thank you.