我具有以下JSonArray,并希望通过其“ ID”搜索值。 这里是存储在QJsonArray中的JSON:
{
"datasources":[
{"id":1, "name":"tps-pos", "display-name":"TPS Position"},
{"id":2, "name":"tps-timer", "display-name":"TPS Timer"},
{"id":3, "name":"laterial-g", "display-name":"Lateral G"},
{"id":4, "name":"longitudal-g", "display-name":"Longitudal G"},
{"id":5, "name":"Z", "display-name":"Z"},
{"id":6, "name":"afr", "display-name":"AFR"},
{"id":135, "name":"IAT", "display-name":"Intake Air temperature"},
{"id":179, "name":"RPM", "display-name":"RPM"}
]}
我为此尝试了std:find,其中m_list包含应该在JSON中找到的ID。
auto result = std::find(m_DataSourceArray.begin(),m_DataSourceArray.end(), m_list[0]);
调试时,我看到“结果”始终与m_DataSourceArray +字段迭代器相同:(调试器视图:)
result @0x7ed9e0 QJsonArray::iterator
a <8 Elemente> QJsonArray
[0] <3 Elemente> QJsonValue (Object)
[1] <3 Elemente> QJsonValue (Object)
[2] <3 Elemente> QJsonValue (Object)
[3] <3 Elemente> QJsonValue (Object)
[4] <3 Elemente> QJsonValue (Object)
[5] <3 Elemente> QJsonValue (Object)
[6] <3 Elemente> QJsonValue (Object)
[7] <3 Elemente> QJsonValue (Object)
i 8 int
答案 0 :(得分:1)
我将创建一个新的搜索功能。喜欢:
QJsonObject findValueFromJsonArray(QJsonArray arr, QString key, int val) {
for (const auto obj : arr) {
if (obj.toObject().value(key) == val)
return obj.toObject();
}
return QJsonObject();
}
现在我可以按ID搜索值了。
QJsonArray m_DataSourceArray = m_DataSourceObject.value(QString("datasources")).toArray();
QJsonObject obj = findValueFromJsonArray(m_DataSourceArray, "id", 10);
// Array doesn't contain a value with id 10
if (obj.isEmpty())
qDebug() << "Empty QJsonObject";
QJsonObject obj2 = findValueFromJsonArray(m_DataSourceArray, "id", 2);
if (!obj2.isEmpty())
qDebug() << "Not empty!";
qDebug() << obj2;
qDebug() << "Display-name of obj2: " << obj2.value("display-name").toString();
qDebug() << findValueFromJsonArray(m_DataSourceArray, "id", 4);
输出:
Empty QJsonObject
Not empty!
QJsonObject({"display-name":"TPS Timer","id":2,"name":"tps-timer"})
Display-name of obj2: "TPS Timer"
QJsonObject({"display-name":"Longitudal G","id":4,"name":"longitudal-g"})
当然,如果您不需要在其他地方进行搜索,则无需创建新函数来进行搜索。
如果需要按名称或显示名称查找值,则可以创建第二个函数。注意val参数现在是QString类型而不是int类型。
QJsonObject findValueFromJsonArray(QJsonArray arr, QString key, QString val) {
for (const auto obj : arr) {
if (obj.toObject().value(key).toString().contains(val, Qt::CaseSensitivity::CaseInsensitive))
return obj.toObject();
}
return QJsonObject();
}
现在,我可以通过其ID或名称搜索值了:
QJsonArray m_DataSourceArray = m_DataSourceObject.value(QString("datasources")).toArray();
QJsonObject obj = findValueFromJsonArray(m_DataSourceArray, "name", "foo");
// Array doesn't contain a value with name 'foo'
if (obj.isEmpty())
qDebug() << "Empty QJsonObject";
QJsonObject obj2 = findValueFromJsonArray(m_DataSourceArray, "name", "tps-pos");
if (!obj2.isEmpty())
qDebug() << "Not empty!";
qDebug() << obj2;
qDebug() << "Display-name of obj2: " << obj2.value("display-name").toString();
qDebug() << findValueFromJsonArray(m_DataSourceArray, "id", 4);
qDebug() << findValueFromJsonArray(m_DataSourceArray, "name", "z");
qDebug() << findValueFromJsonArray(m_DataSourceArray, "display-name", "rpm");
输出:
Empty QJsonObject
Not empty!
QJsonObject({"display-name":"TPS Position","id":1,"name":"tps-pos"})
Display-name of obj2: "TPS Position"
QJsonObject({"display-name":"Longitudal G","id":4,"name":"longitudal-g"})
QJsonObject({"display-name":"Z","id":5,"name":"Z"})
QJsonObject({"display-name":"RPM","id":179,"name":"RPM"})