在 Mac 中使用 QJSON - 遇到一些问题

发布于 2024-09-24 00:09:42 字数 1723 浏览 2 评论 0原文

我正在使用 QJson 进行解析。但我遇到了一些问题。我使用了以下代码:

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");  
    }  
}  

输出是:

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

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  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屌丝范 2024-10-01 00:09:42

readAll 函数的结果是一个空字节数组。根据文档,这可能意味着没有可用数据或发生错误。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文