10-03-2012 08:53 PM
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","tempave
.....
]
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.
Solved! Go to Solution.
10-03-2012 11:07 PM
I suggest looking at this tutorial, its for making HTTP requests.
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);
10-04-2012 02:37 AM
10-04-2012 02:38 AM
10-04-2012 10:20 AM
you can also just pass a QNetworkReply to the load method of jsondataaccess jda.load(reply)
10-04-2012 10:56 PM
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
01-24-2013 02:03 AM
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
01-25-2013 06:10 PM
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::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.