10-09-2012 07:24 AM - edited 10-09-2012 07:49 AM
Hi,
iam trying to load a json file that ive downloaded. (download works, checked if file exists in my "data/blabla.json")
But now when i'm trying to use JsonDataAccess it returns the following error while building
expected ';' before 'jda' OR 'JsonDataAccess' was not declared in this scope
the only thing i did:
#include <bb/data/JsonDataAccess.hpp>
/* ... */
void app::parseJson(){
if (json->exists()) { JsonDataAccess jda; }
}
Does anybody have a idea?
Thx
// EDIT
after adding
using namespace bb::data;
it worked!
Solved! Go to Solution.
10-09-2012 08:02 AM
10-09-2012 08:05 AM
yeah but what i think is strange is that i tried to do it like this bb:data:JsonDataAccess but this didn't work..
a well it is working now ![]()
10-09-2012 10:33 AM
probably because it would have to be bb::data::JsonDataAccess not bb:data:JsonDataAccess ![]()
I swear, the number of questions posted on these forums that are solved by namespace issues goes up every day
10-09-2012 11:41 AM
o lol
yeah i know made a mistake in the quickreply.
10-10-2012 05:22 AM
Hi Friend,
can you please post your source code ?
Because i want to take idea how you download json and write the file , please help me
10-10-2012 06:58 AM - edited 10-10-2012 07:27 AM
Sure,
In your header file register the slots and the vars
private slots:
void requestFinished(QNetworkReply *reply);
private:
QNetworkAccessManager *mNetworkAccessManager;
QFile *file;
for example (pseudo code)
// App.h
#ifndef APP_H_ #define APP_H_
//* includes & namespaces here *//
class App{
public: App();
Q_INVOKABLE void init(); private slots: void requestFinished(QNetworkReply *reply); private: QNetworkAccessManager *mNetworkAccessManager; QFile *json; }; #endif /* APP_H_ */
Then in your constructor create the network access manager like this
mNetworkAccessManager = new QNetworkAccessManager(this);
then add the signal and slots and make sure to check them after that
bool result = connect(mNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*))); Q_ASSERT(result); Q_UNUSED(result);
what i did was create the file refference here
json = new QFile("data/file.json"); then call the init method to start the request, i think the Q_INVOKABLE is only needed when you call the init method from QML
init();
// init method looks like this
void App::init(){
QNetworkRequest request = QNetworkRequest();
request.setUrl(QUrl("url to json"));
mNetworkAccessManager->get(request);
}
and finaly in the SLOT we used requestFinished(QNetworkReply *reply) so this is how this method looks like
void App::requestFinished(QNetworkReply *reply){
qDebug("Request finished");
qDebug() << reply->error();
qDebug() << reply->errorString();
if (reply->error() == QNetworkReply::NoError) {
qDebug() << "No error";
QByteArray data = reply->readAll();
if (!json->open(QIODevice::ReadWrite)) {
qDebug() << "Failed to open file";
return;
}
json->write(data);
json->flush();
json->close();
qDebug() << "JSON file fetched and saved to 'data/file.json'";
}
Hope this helps.
Greets,
Xtravanta
10-10-2012 07:33 AM
Thanks Xtravanta
10-10-2012 07:55 AM
no problem ![]()