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
robtraf
Posts: 48
Registered: ‎03-27-2011
My Device: BlackBerry Z10
Accepted Solution

Parse Json from Internet HTTP call

Hi, I have played around with the sample Cascades weatherguesser app. In the app a pre packaged Json file is used to obtain data to populate a listview.

In my example app, I have made a HTTP request to return Json data. Do I have to store this data in a file on my local drive to read or can I read data directly from the HTTP call?

 

Also do I need to base my Json file on the one provided with weatherguesser Json file?.  The reason is that I can parse the Json file from weatherguesser but unable to parse from HTTP requested Json data.

 

Json file weatherguesser app -

[

{"region":"Europa","city":"Malmö","date":"1984 1 1","icon":"3","temphi":"14","templo":"-2","tempaverage":"8"},

.....

]

 

My Json file - 

{ "data": { "current_condition": [ {"cloudcover": "0", "humidity": "55"........

 

I am using -

 

JsonDataAccess jda;

QVariantList weatherData;

 

  if (mReply->error() == QNetworkReply::NoError)

  {

    // Load the data using the reply QIODevice.

 

    weatherData = jda.load(mReply).value<QVariantList>();

 

  }

 

Thank you.

 

Please use plain text.
Regular Contributor
CrozyBB
Posts: 83
Registered: ‎07-19-2012
My Device: Bold 9790, Dev Alpha A

Re: Parse Json from Internet HTTP call

I suggest looking at this tutorial, its for making HTTP requests.

https://developer.blackberry.com/cascades/documentation/device_comm/networking/tutorial_http_comm.ht...

 

Your reply will be need to be converted to a string which I've done before using the following code:

QString strReply = QString::fromUtf8(reply->readAll());

Next you can load straight into your QVariant from the buffer using this:

 

QVariant jsonva = ja.loadFromBuffer(strReply);

 

Please use plain text.
Developer
robtraf
Posts: 48
Registered: ‎03-27-2011
My Device: BlackBerry Z10

Re: Parse Json from Internet HTTP call

Thanks. Worked well. Just changed ja to ida.
Please use plain text.
Developer
robtraf
Posts: 48
Registered: ‎03-27-2011
My Device: BlackBerry Z10

Re: Parse Json from Internet HTTP call

Sorry meant jda
Please use plain text.
Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900

Re: Parse Json from Internet HTTP call

you can also just pass a QNetworkReply to the load method of jsondataaccess jda.load(reply)

Like all of my posts
Please use plain text.
Developer
robtraf
Posts: 48
Registered: ‎03-27-2011
My Device: BlackBerry Z10

Re: Parse Json from Internet HTTP call

Great help guys. I'm at the stage to extract the data fron the Json file and have read up on all the relevant information in the documentation.

My extracted Json data into the buffer looks like this :-

 

QVariant(QVariantMap, QMap(("data", QVariant(QVariantMap, QMap(("current", QVariant(QVariantList, (QVariant(QVariantMap, QMap(("speed", QVariant(QString, "100") ) ("time", QVariant(QString, "10:10 AM").......

 

I need to extract the speed and its value, time and its value and so on.

 

I have tried numerous coding examples with no luck. Just needed the code to load the above into my model,  GroupDataModel.

 

Thank you

Please use plain text.
Developer
dcharya
Posts: 74
Registered: ‎03-04-2012
My Device: developer

Re: Parse Json from Internet HTTP call

Hey Hi,

 

I was tiring this solution but getting some problem. The problem is when I am trying to build my project it gives error stating that "expected ';' before 'ja" when I declare JsonDataAccess .

 

Here is my code snippet :

 

JsonDataAccess ja;
QString response;
if (reply->error() == QNetworkReply::NoError) {
const int available = reply->bytesAvailable();
if (available > 0) {
response = QString::fromUtf8(reply->readAll());
qDebug() << "Tag Name is :: "<<response;
QVariant jsonva = ja.loadFromBuffer(response);

}

 

Regards
Deepak Kumar

Deepak Charya
Please use plain text.
Developer
robtraf
Posts: 48
Registered: ‎03-27-2011
My Device: BlackBerry Z10

Re: Parse Json from Internet HTTP call

Hi Deepak,

Here is my code that I used:

 

JsonDataAccess jda;

QVariant jsonData;

 

if (mReply->error() == QNetworkReply::NoError)

{

 

jsonData = jda.load(mReply).value<QVariant>();

 

 

}

 

if (jda.hasError()) {

bb::data&colon;:DataAccessError error = jda.error();

qDebug() << "JSON loading error: " << error.errorType() << ": "

<< error.errorMessage();

return;

}

 

Also do not forget to include - 

 

#include<bb/data/JsonDataAccess>

 

Hope this helps.

 

Please use plain text.