Qt QJson解析json数据

无人久伴 提交于 2019-12-04 18:28:28

Qt QJson解析json数据

//加载根目录文件

void TeslaManageData::loadRootFolderFiles()
{
    QNetworkAccessManager *manager = new QNetworkAccessManager(this);;
    QNetworkRequest network_request;
    QByteArray post_data;

    network_request.setUrl(QUrl("http://121.60.49.57:9080/tsinghuaVCService/init/selectAllFolder.action"));
    network_request.setHeader(QNetworkRequest::UserAgentHeader, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.4098.3 Safari/537.36");

    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));

    //发送请求
    manager->get(network_request);
}

 

void TeslaManageData::replyFinished(QNetworkReply* networkReply)
{
    QTextCodec *codec = QTextCodec::codecForName("utf8");
    QByteArray allData = networkReply->readAll();
    //使用utf8编码,这样才可以显示中文  
    QString all = codec->toUnicode(allData);
    
    QJsonParseError jsonError;
    QJsonDocument doucment = QJsonDocument::fromJson(allData, &jsonError);  // 转化为 JSON 文档
    if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) {  // 解析未发生错误
        QJsonArray jsonArray1 = doucment.array();
        
        QJsonArray::Iterator iter1;
        for (iter1 = jsonArray1.begin();iter1 != jsonArray1.end();++iter1)
        {
            QJsonObject jsonObject1 = (*iter1).toObject();
            parseJsonObjectI(jsonObject1);
        }
    }
    networkReply->deleteLater();   
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!