PDA

View Full Version : Qjson



Girija
17th September 2010, 08:14
Hi,
I am using the QJson for parsing. But I am stuck up with some issues. I have used the following code.

void CityBook ::getCityList(QUrl url)
{
//!connect(cityReply, SIGNAL(readyRead()),this, SLOT(httpReadyRead()));
cityGuideNetworkAccessManager = new QNetworkAccessManager(this);
connect(cityGuideNetworkAccessManager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(httpReadyRead(QNetworkReply*)));
QNetworkRequest cityRequest(url);
cityGuideNetworkAccessManager->get(cityRequest);
}

void CityBook::httpReadyRead(QNetworkReply *reply)
{
QMessageBox::information(this, tr("HTTP"),
tr(reply->readAll()),QMessageBox::NoButton
);
QJson::Parser parser;
bool ok;
const QByteArray &resultbyte = reply->readAll();
qDebug() << resultbyte;
QVariant result1 = parser.parse(reply->readAll(), &ok);
qDebug() << result1;
QVariantList result=parser.parse(resultbyte,&ok).toList();
qDebug()<< result.size();
if (!ok)
{
qFatal("An error occurred during parsing");
exit (1);
}
qDebug() <<"error String"<< parser.errorString();
qDebug() <<"error" //! QVariantList entries = result["name"].toList();
foreach (QVariant city, result) {
QVariantMap names = city.toMap();
qDebug() << "\t-" << names.value("name");
}
}

The output is

Starting /Users/QT Developement/CityBook-build-desktop/CityBook.app/Contents/MacOS/CityBook...
""
QVariant(, )
0
error String ""
error 0

Please help me .. Where I have done mistake… Thanks is advance.

tbscope
17th September 2010, 08:30
const QByteArray &resultbyte = reply->readAll();

What does this mean and do?

Girija
17th September 2010, 09:02
The meaning of the QByteArray is to provide the array of the bytes. Here The reply->readAll returns the byte array.

tbscope
17th September 2010, 09:07
I know what a byte array is.

I want to read from you what this line exactly does. I know what it does, but I want you to see the mistakes.

Girija
17th September 2010, 09:18
Sorry. here i just assign the result to that particular variable.

tbscope
17th September 2010, 09:20
What happens if you use

QByteArray resultbyte = reply->readAll();?

Girija
17th September 2010, 10:16
I am also getting the same output.

Girija
17th September 2010, 10:51
Hi,

I can see the parsing the result. Just I made one chance in the code based on the following link http://stackoverflow.com/questions/3732998/qjson-using-in-mac-getting-some-issues.
It is working fine.

Thank u very much tbscope..

Thanks a lot.

tbscope
17th September 2010, 15:19
Ohh, I didn't see the first readAll()

Please use the code tags to format your code.