使用QT C ++从json获取多个数据

时间:2017-09-12 13:01:31

标签: c++ json qt api

我需要从json获取多个数据,我可以阅读的第一部分,但我内部的json无法得到它。

所以来自API的数据:

enter image description here

我在c ++ / QT中的代码是下一个:

QString JsonFunctions::getUnits(QString token_type, QString access_token)
{
    QString lista;
    QEventLoop eventLoop;
    QNetworkAccessManager mgr;
    QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));

    QJsonObject json;

    QNetworkRequest request(QUrl("https://MYURL/api/v2/units"));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    //request.setRawHeader("Content-Type", "application/json");
    request.setRawHeader("Accept", "application/json");
    QString sing =  token_type + " " + access_token;
    request.setRawHeader("Authorization", sing.toUtf8());

    QSslConfiguration conf = request.sslConfiguration();
    conf.setPeerVerifyMode(QSslSocket::VerifyNone);
    request.setSslConfiguration(conf);

    QNetworkReply *reply = mgr.get(request);
    eventLoop.exec();

    QString strReply = (QString)reply->readAll();
    qDebug() << "reply" << strReply;
    qDebug() << "code" << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();

    QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
    QJsonArray jsonArray = jsonResponse.array();

    QJsonObject jsonObject = jsonResponse.object();
    QVariantMap mainmap = jsonObject.toVariantMap();
    QVariantList phraseList = mainmap["data"].toList();

    QJsonObject jsonobjs = QJsonObject::fromVariantMap(mainmap);
    QJsonArray dataObject = jsonobjs.value("data").toArray();

    for(int i = 0; i < dataObject.count(); i++){
        QJsonObject temp = dataObject[i].toObject();
        units unitsTemp;

        unitsTemp.id = temp.value("ac_role_name").toString();
        unitsTemp.description = temp.value("description").toString();
        unitsTemp.thing_name = temp.value("thing_name").toString();
        unitsTemp.serial_number = temp.value("serial_number").toString();
        unitsTemp.access_key = temp.value("access_key").toString();
        unitsTemp.state = temp.value("state").toInt();
        unitsTemp.location_id = temp.value("location_id").toString();
        unitsTemp.billing_id = temp.value("billing_id").toString();
        unitsTemp.created_at = temp.value("created_at").toString();
        unitsTemp.update_at = temp.value("update_at").toString();
        unitsTemp.nickname = temp.value("nickname").toString();
        unitsTemp.image = temp.value("image").toString();
        unitsTemp.status = temp.value("status").toString();
        unitsTemp.ac_role_name = temp.value("ac_role_name").toString();

        //QJsonValue siteValue = temp.value("location").toArray();
        //QJsonObject locationObject = siteValue.toObject();

        //How to get location data ?

        QJsonArray dataObjectz = temp.value("location").toArray().at(0).toArray();

        //unitsTemp.locations.address = locationObject.value("address").toString();

        //devo prendere le location

        QString debug;
    }

    return lista;
}

我尝试过代码和代码......但是如何获取位置数据?我没有发现解决方案适合我。 任何的想法 ? 我是qt和c ++的新手。感谢。

1 个答案:

答案 0 :(得分:0)

解决方案:

QJsonObject locationObject = temp["location"].toObject();
QString addressFromLocation = locationObject.value("address").toString();

为我工作