我正在使用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].ID,properties[x].PropertyName和properties[x].keyQt中?
properties[x].ID
properties[x].PropertyName
properties[x].key
编辑:
使用QScriptEngine我尝试了这个:
QScriptEngine
QString data = (QString)reply->readAll(); QScriptEngine engine; QScriptValue result = engine.evaluate(data); qDebug() << result.toString();
调试说“ SyntaxError:解析错误”
我想到了:
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()); }