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

Posts: 136
Registered: ‎01-25-2010
My Device: Z30, Z10, Q10, Dev Alpha B & Bold 9900
My Carrier: Bell
Accepted Solution

QNetworkReply running into issue loading JSON data



I'm a newby with C++ and QT but so far I'm starting to like Cascade NDK !!!  


I'm trying to load a json data file which is fetch via a http request. Everything goes throught but my json data would just not load into the QVariantList. So after a few hours of poking arround, I finally noticed that the json data that is returned from the http request is missing two brakets [] (one @ start and one @ end).

When I load the json data from a file with the two brakets included, the QVariantList loads properly and I can debug throught the records...


Now my question is... from C++ how can I add those to [ ] brakets... See sample code below :


void MyJSONReadClass::httpFinished()
  JsonDataAccess jda;
  QVariantList myDataList;

  if (mReply->error() == QNetworkReply::NoError)
    // Load the data using the reply QIODevice.
    qDebug() << mReply;
    myDataList = jda.load(mReply).value<QVariantList>();
    // Handle error

  if (jda.hasError())
    bb::data&colon;:DataAccessError error = jda.error();
    qDebug() << "JSON loading error: " << error.errorType() << ": "
        << error.errorMessage();


  // The reply is not needed now so we call deleteLater() function since we are in a slot.



Also, I would have thought that the jda.hasError() would have capture this issue... but  guess not !


Am I using the wrong approach or wrong classes ??? The base example used is the WeatherGuesser project.


Thanks for you help...

Posts: 110
Registered: ‎10-22-2011
My Device: PlayBook, Bold 9650, Dev Alpha A
My Carrier: Sprint

Re: QNetworkReply running into issue loading JSON data

You might want to look into the sample code provided by RIM. In this case it seems the JSON Read/Write sample is a good example to learn from.


Most of its C++ work is done in app.cpp:




I personally suggest cloning Cascades-Samples into Momentics to look at sources rather than trying to use the GitHub views, btw. Smiley Happy

Is there a helpful or useful post in this thread? Click the thumbs up on it so that other people can find it more easily!
Like this post too! Smiley Very Happy
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: QNetworkReply running into issue loading JSON data

This might be unrelated to brackets. Try fetching the data from QNetworkResponse as QByteArray then load it into JsonDataAccess using loadFromBuffer function:


 myDataList = jda.loadFromBuffer(mReply.readAll()).value<QVariantList>();


If this won't help, you can add brackets this way (untested, please check the docs for proper function names if this won't compile):


QByteArray a = mReply.readAll();
a.insert(0, '[');
myDataList = jda.loadFromBuffer(a).value<QVariantList>();

Note that if response data is zero terminated (most likely it isn't, but there's a possibility of this), you'll have to check if last symbol in byte array is '\0' and insert bracket before it.


Docs for QByteArray:


Andrey Fidrya, @zmeyc on twitter
Posts: 136
Registered: ‎01-25-2010
My Device: Z30, Z10, Q10, Dev Alpha B & Bold 9900
My Carrier: Bell

Re: QNetworkReply running into issue loading JSON data

Thanks Zmey... I had to manually add the brakets... Now everything is working Smiley Happy