一尘不染

Qt使用QJsonDocument,QJsonObject,QJsonArray解析JSON

json

我正在使用Qt5。我正在尝试从json对象获取值。这是我试图从中获取数据的json对象的外观:

{
    "success": true,
    "properties": [
        {
            "ID": 1001,
            "PropertyName": "McDonalds",
            "key": "00112233445566778899aabbccddeeff"
        },
        {
            "ID": 1002,
            "PropertyName": "Burger King",
            "key": "10112233445566778899aabbccddeeff"
        },
        {
            "ID": 1003,
            "PropertyName": "Taco Bell",
            "key": "20112233445566778899aabbccddeeff"
        }
    ]
}

我怎样才能创建一个包含三个阵列properties[x].IDproperties[x].PropertyNameproperties[x].keyQt中?

编辑:

使用QScriptEngine我尝试了这个:

QString data = (QString)reply->readAll();

QScriptEngine engine;

QScriptValue result = engine.evaluate(data);

qDebug() << result.toString();

调试说“ SyntaxError:解析错误”


阅读 899

收藏
2020-07-27

共1个答案

一尘不染

我想到了:

QStringList propertyNames;
QStringList propertyKeys;
QString strReply = (QString)reply->readAll();
QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
QJsonObject jsonObject = jsonResponse.object();
QJsonArray jsonArray = jsonObject["properties"].toArray();

foreach (const QJsonValue & value, jsonArray) {
    QJsonObject obj = value.toObject();
    propertyNames.append(obj["PropertyName"].toString());
    propertyKeys.append(obj["key"].toString());
}
2020-07-27