I'm using Qt5. I am trying to obtain values from a json object. Here is what the json object looks like that I am trying to get data from:
我用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"
}
]
}
How can I create three arrays that contain properties[x].ID, properties[x].PropertyName, and properties[x].key in Qt?
如何创建包含属性[x]的三个数组。ID属性(x)。PropertyName和属性[x]。在Qt的关键吗?
Edit:
编辑:
Using QScriptEngine I tried this:
使用QScriptEngine我试过:
QString data = (QString)reply->readAll();
QScriptEngine engine;
QScriptValue result = engine.evaluate(data);
qDebug() << result.toString();
Debug is saying "SyntaxError: Parse error"
调试是说“SyntaxError: Parse error”
2 个解决方案
#1
43
I figured it out:
我想通了:
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());
}
#2
1
Here is an example from How To Manipulate JSON With C++ and Qt.
下面是如何使用c++和Qt操作JSON的示例。
// reads a json file from disk to QVariantMap
// originally from http://erickveil.github.io/2016/04/06/How-To-Manipulate-JSON-With-C++-and-Qt.html
bool readJsonFile(std::string file_path, QVariantMap& result)
{
// step 1
QFile file_obj(QString::fromStdString(file_path));
if (!file_obj.open(QIODevice::ReadOnly)) {
std::cout << "Failed to open " << file_path << std::endl;
exit(1);
}
// step 2
QTextStream file_text(&file_obj);
QString json_string;
json_string = file_text.readAll();
file_obj.close();
QByteArray json_bytes = json_string.toLocal8Bit();
// step 3
auto json_doc = QJsonDocument::fromJson(json_bytes);
if (json_doc.isNull()) {
std::cout << "Failed to create JSON doc." << std::endl;
return false;
}
if (!json_doc.isObject()) {
std::cout << "JSON is not an object." << std::endl;
return false;
}
QJsonObject json_obj = json_doc.object();
if (json_obj.isEmpty()) {
std::cout << "JSON object is empty." << std::endl;
return false;
}
// step 4
result = json_obj.toVariantMap();
return true;
}
// writes a QVariantMap to disk
bool writeJsonFile(QVariantMap point_map, std::string file_path)
{
QJsonObject json_obj = QJsonObject::fromVariantMap(point_map);
QJsonDocument json_doc(json_obj);
QString json_string = json_doc.toJson();
QFile save_file(QString::fromStdString(file_path));
if (!save_file.open(QIODevice::WriteOnly)) {
std::cout << "failed to open save file" << std::endl;
return false;
}
save_file.write(json_string.toLocal8Bit());
save_file.close();
return true;
}
#1
43
I figured it out:
我想通了:
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());
}
#2
1
Here is an example from How To Manipulate JSON With C++ and Qt.
下面是如何使用c++和Qt操作JSON的示例。
// reads a json file from disk to QVariantMap
// originally from http://erickveil.github.io/2016/04/06/How-To-Manipulate-JSON-With-C++-and-Qt.html
bool readJsonFile(std::string file_path, QVariantMap& result)
{
// step 1
QFile file_obj(QString::fromStdString(file_path));
if (!file_obj.open(QIODevice::ReadOnly)) {
std::cout << "Failed to open " << file_path << std::endl;
exit(1);
}
// step 2
QTextStream file_text(&file_obj);
QString json_string;
json_string = file_text.readAll();
file_obj.close();
QByteArray json_bytes = json_string.toLocal8Bit();
// step 3
auto json_doc = QJsonDocument::fromJson(json_bytes);
if (json_doc.isNull()) {
std::cout << "Failed to create JSON doc." << std::endl;
return false;
}
if (!json_doc.isObject()) {
std::cout << "JSON is not an object." << std::endl;
return false;
}
QJsonObject json_obj = json_doc.object();
if (json_obj.isEmpty()) {
std::cout << "JSON object is empty." << std::endl;
return false;
}
// step 4
result = json_obj.toVariantMap();
return true;
}
// writes a QVariantMap to disk
bool writeJsonFile(QVariantMap point_map, std::string file_path)
{
QJsonObject json_obj = QJsonObject::fromVariantMap(point_map);
QJsonDocument json_doc(json_obj);
QString json_string = json_doc.toJson();
QFile save_file(QString::fromStdString(file_path));
if (!save_file.open(QIODevice::WriteOnly)) {
std::cout << "failed to open save file" << std::endl;
return false;
}
save_file.write(json_string.toLocal8Bit());
save_file.close();
return true;
}