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