Qt Json 解析c++

Qt Json Parsing c++

我正在尝试从 api 解析 QT 中的 json 对象。但是,当我尝试下面编写的代码时,我无法解析该对象。我只是想获取存储在这些参数中的信息。

API 是

{
    "error": {
        "errorcode": 0,
        "errorstring": ""
    },
    "login": 1,
    "logintoken": "209b06aa7f059673db393ff7a731af1847344903b9733f026cc3a6f7a5b797b3"
}

代码是

  QUrl ava_url("http://pinundpin.de/wsdemo/atgdemoapi_v3/index.php");
QNetworkRequest ava_request(ava_url);
ava_request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");

QNetworkAccessManager *manager = new QNetworkAccessManager();
QEventLoop loop;

QObject::connect(manager,
                 SIGNAL(finished(QNetworkReply *)),
                 &loop,
                 SLOT(quit()));
QByteArray postData;
Username = "testwebser";
Passwd = "2017@QWEasdZXC";
postData.append("action=login&");
postData.append("password="+Passwd+"&");
postData.append("username="+Username);

QNetworkReply* reply = manager->post(ava_request,postData);
loop.exec();

QString response = (QString)reply->readAll();
qDebug()<< response;

QJsonDocument temp = QJsonDocument::fromJson(response.toUtf8());

 QJsonObject jsonObj = temp.object();
 qDebug() <<"error"<< jsonObj["error"].toString();
 qDebug() <<"login"<< jsonObj["login"].toString();
 qDebug() << "logintoken"<<jsonObj["logintoken"].toString();

响应字符串看起来

输出为

D/libAndroisShop.so(21944): ../AndroisShop/networkconnection.cpp:45 (QString NetworkConnection::Connect(QString, QString)): "<br><br>NO OF ROWDATA: 1{\"error\":{\"errorcode\":0,\"errorstring\":\"\"},\"login\":1,\"logintoken\":\"4daaf6b3dd5a26a2ad2c436e564bfa4d6c439ce4d0d6cd66705a8bdadddddaa0\"}"
D/libAndroisShop.so(21944): ../AndroisShop/networkconnection.cpp:50 (QString NetworkConnection::Connect(QString, QString)): error ""
D/libAndroisShop.so(21944): ../AndroisShop/networkconnection.cpp:51 (QString NetworkConnection::Connect(QString, QString)): login ""
D/libAndroisShop.so(21944): ../AndroisShop/networkconnection.cpp:52 (QString NetworkConnection::Connect(QString, QString)): logintoken ""

邮递员图片

如果您在原始模式下查看 POSTMAN 的结果,您会得到以下内容:

QNAM 得到什么但 POSTMAN 有另一种算法来提取 json:

因此一个可能的解决方案是删除不需要的元素:<br><br>NO OF ROWDATA: 1,为此它利用了 json 必须以“{”开头的优势,因此使用了上级子字符串。

QUrl ava_url("http://pinundpin.de/wsdemo/atgdemoapi_v3/index.php");
QNetworkRequest ava_request(ava_url);
ava_request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");

QNetworkAccessManager *manager = new QNetworkAccessManager();
QEventLoop loop;

QObject::connect(manager,
                 &QNetworkAccessManager::finished,
                 &loop,
                 &QEventLoop::quit);
QByteArray postData;
QString Username = "testwebser";
QString Passwd = "2017@QWEasdZXC";
postData.append("action=login&");
postData.append("password="+Passwd+"&");
postData.append("username="+Username);

QNetworkReply *reply = manager->post(ava_request,postData);
loop.exec();
QByteArray response = reply->readAll();
QByteArray json = response.mid(response.indexOf("{"));
QJsonDocument temp = QJsonDocument::fromJson(json);
QJsonObject jsonObj = temp.object();
QJsonObject errorObj = jsonObj["error"].toObject();
qDebug()<<"error: "
       <<"errorcode: "<< errorObj["errorcode"].toInt()
       <<"errorstring: "<< errorObj["errorstring"].toString();
qDebug() <<"login"<< jsonObj["login"].toInt();
qDebug() << "logintoken"<<jsonObj["logintoken"].toString();

输出:

error:  errorcode:  0 errorstring:  ""
login 1
logintoken "cc7e46f34e0a268cecbaaeaad41b0ae2727cc7196b632574a4db16544576d012"