QJSON using in Mac - getting some issues

2019-05-28 12:48发布

问题:

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" <parser.errorLine();  
    //! 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  

回答1:

The result of the readAll function is an empty byte array. According to documentation this can mean either that no data are available or that an error occurred.



标签: qt qt4 qjson