class.cpp
Raw
#include <iostream>
#include <string>
#include <nlohmann/json.hpp> // 引入 nlohmann/json 库
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QObject>
#include <QString>
#include <QDebug>
using json = nlohmann::json;
class YourCppClass : public QObject {
Q_OBJECT
public:
YourCppClass();
void fetchData(const QString &url);
private slots:
void onReplyFinished(QNetworkReply* reply);
private:
QNetworkAccessManager* networkManager;
};
YourCppClass::YourCppClass() {
networkManager = new QNetworkAccessManager(this);
connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onReplyFinished(QNetworkReply*)));
}
void YourCppClass::fetchData(const QString &url) {
QNetworkRequest request(QUrl(url));
networkManager->get(request);
}
void YourCppClass::onReplyFinished(QNetworkReply* reply) {
if (reply->error() == QNetworkReply::NoError) {
QByteArray responseData = reply->readAll();
QString responseStr(responseData);
// 将 QByteArray 转换为 std::string 以供 nlohmann/json 使用
std::string responseDataStr = responseStr.toStdString();
try {
// 使用 nlohmann/json 解析 JSON 数据
json jsonObj = json::parse(responseDataStr);
// 解析出数据 (根据实际的 JSON 格式)
std::string username = jsonObj["username"].get<std::string>();
std::string content = jsonObj["content"].get<std::string>();
qDebug() << "Username:" << QString::fromStdString(username);
qDebug() << "Content:" << QString::fromStdString(content);
} catch (json::parse_error& e) {
qDebug() << "JSON parse error:" << e.what();
}
} else {
qDebug() << "Network error occurred:" << reply->errorString();
}
reply->deleteLater();
}
int main(int argc, char *argv[]) {
// 简单示例程序初始化
QCoreApplication app(argc, argv);
YourCppClass yourClass;
yourClass.fetchData("http://example.com/api"); // 替换为实际的 API URL
return app.exec();
}
#include "YourCppClass.moc"
| 1 | #include <iostream> |
| 2 | #include <string> |
| 3 | #include <nlohmann/json.hpp> // 引入 nlohmann/json 库 |
| 4 | #include <QNetworkAccessManager> |
| 5 | #include <QNetworkRequest> |
| 6 | #include <QNetworkReply> |
| 7 | #include <QObject> |
| 8 | #include <QString> |
| 9 | #include <QDebug> |
| 10 | |
| 11 | using json = nlohmann::json; |
| 12 | |
| 13 | class YourCppClass : public QObject { |
| 14 | Q_OBJECT |
| 15 | |
| 16 | public: |
| 17 | YourCppClass(); |
| 18 | void fetchData(const QString &url); |
| 19 | |
| 20 | private slots: |
| 21 | void onReplyFinished(QNetworkReply* reply); |
| 22 | |
| 23 | private: |
| 24 | QNetworkAccessManager* networkManager; |
| 25 | }; |
| 26 | |
| 27 | YourCppClass::YourCppClass() { |
| 28 | networkManager = new QNetworkAccessManager(this); |
| 29 | connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onReplyFinished(QNetworkReply*))); |
| 30 | } |
| 31 | |
| 32 | void YourCppClass::fetchData(const QString &url) { |
| 33 | QNetworkRequest request(QUrl(url)); |
| 34 | networkManager->get(request); |
| 35 | } |
| 36 | |
| 37 | void YourCppClass::onReplyFinished(QNetworkReply* reply) { |
| 38 | if (reply->error() == QNetworkReply::NoError) { |
| 39 | QByteArray responseData = reply->readAll(); |
| 40 | QString responseStr(responseData); |
| 41 | |
| 42 | // 将 QByteArray 转换为 std::string 以供 nlohmann/json 使用 |
| 43 | std::string responseDataStr = responseStr.toStdString(); |
| 44 | |
| 45 | try { |
| 46 | // 使用 nlohmann/json 解析 JSON 数据 |
| 47 | json jsonObj = json::parse(responseDataStr); |
| 48 | |
| 49 | // 解析出数据 (根据实际的 JSON 格式) |
| 50 | std::string username = jsonObj["username"].get<std::string>(); |
| 51 | std::string content = jsonObj["content"].get<std::string>(); |
| 52 | |
| 53 | qDebug() << "Username:" << QString::fromStdString(username); |
| 54 | qDebug() << "Content:" << QString::fromStdString(content); |
| 55 | |
| 56 | } catch (json::parse_error& e) { |
| 57 | qDebug() << "JSON parse error:" << e.what(); |
| 58 | } |
| 59 | } else { |
| 60 | qDebug() << "Network error occurred:" << reply->errorString(); |
| 61 | } |
| 62 | |
| 63 | reply->deleteLater(); |
| 64 | } |
| 65 | |
| 66 | int main(int argc, char *argv[]) { |
| 67 | // 简单示例程序初始化 |
| 68 | QCoreApplication app(argc, argv); |
| 69 | |
| 70 | YourCppClass yourClass; |
| 71 | yourClass.fetchData("http://example.com/api"); // 替换为实际的 API URL |
| 72 | |
| 73 | return app.exec(); |
| 74 | } |
| 75 | |
| 76 | #include "YourCppClass.moc" |
| 77 |