2026/2/13 8:30:51
网站建设
项目流程
.net网站开发实验报告,宝安中心做网站,室内设计师培训装潢设计培训,网站建设和购买区别一 概述 对于纯数组嵌套的JSON数据#xff0c;QJsonArray完全支持嵌套查找#xff0c;可以通过多层索引逐层访问。由于没有对象键名#xff0c;查找完全依赖于数组索引。二 纯数组嵌套JSON示例[[[a, b, c],[d, eQJsonArray完全支持嵌套查找可以通过多层索引逐层访问。由于没有对象键名查找完全依赖于数组索引。二 纯数组嵌套JSON示例[[[a, b, c],[d, e, f]],[[100, 200, 300],[400, 500, 600]],[[true, false],[null, text]]]三 基本嵌套查找方法1 直接索引访问#include QJsonDocument#include QJsonArray// 解析纯数组JSONQJsonDocument doc QJsonDocument::fromJson([ [[1,2],[3,4]], [[5,6],[7,8]] ]);QJsonArray rootArray doc.array();// 逐层索引访问if (rootArray.size() 0 rootArray[0].isArray()) {QJsonArray secondLevel rootArray[0].toArray();if (secondLevel.size() 1 secondLevel[1].isArray()) {QJsonArray thirdLevel secondLevel[1].toArray();if (thirdLevel.size() 0) {// 访问 [0][1][0] 的值QJsonValue value thirdLevel[0];qDebug() 值: value.toInt(); // 输出: 3}}}2 使用辅助函数进行路径查找/*** 通过整数索引路径查找纯数组中的值* param array 根数组* param indexPath 索引路径如 [0,1,0] 表示 array[0][1][0]* return 找到的值未找到返回 QJsonValue::Undefined*/QJsonValue findInNestedArray(const QJsonArray array, const QVectorint indexPath) {QJsonValue current array;for (int index : indexPath) {if (current.isArray()) {QJsonArray currentArray current.toArray();if (index 0 index currentArray.size()) {current currentArray.at(index);} else {return QJsonValue::Undefined; // 索引越界}} else {return QJsonValue::Undefined; // 路径中间不是数组}}return current;}// 使用示例QVectorint path {0, 1, 0}; // 查找 [0][1][0]QJsonValue result findInNestedArray(rootArray, path);if (!result.isUndefined()) {qDebug() 找到的值: result.toInt();}3 使用字符串路径点分隔索引/*** 使用点分隔的字符串路径查找如 0.1.0*/QJsonValue findInArrayByStringPath(const QJsonArray array, const QString path) {QStringList indexStrings path.split(.);QJsonValue current array;for (const QString indexStr : indexStrings) {bool ok;int index indexStr.toInt(ok);if (!ok || index 0) {return QJsonValue::Undefined; // 无效的索引格式}if (current.isArray()) {QJsonArray currentArray current.toArray();if (index currentArray.size()) {current currentArray.at(index);} else {return QJsonValue::Undefined; // 索引越界}} else {return QJsonValue::Undefined; // 中间不是数组}}return current;}// 使用示例QJsonValue value findInArrayByStringPath(rootArray, 0.1.0);if (!value.isUndefined()) {qDebug() 值: value.toString(); // 如果是字符串数组}四 实际应用场景1 多维矩阵/表格数据// 表示3x3矩阵QString jsonStr R([[1, 2, 3],[4, 5, 6],[7, 8, 9]]);QJsonDocument doc QJsonDocument::fromJson(jsonStr.toUtf8());QJsonArray matrix doc.array();// 获取第2行第3列索引 [1][2]QJsonValue element findInArrayByStringPath(matrix, 1.2);if (!element.isUndefined()) {qDebug() 矩阵[1][2] element.toInt(); // 输出: 6}2 三维坐标数据// 多个三维坐标点QString coordsJson R([[[10, 20, 30], [40, 50, 60]],[[70, 80, 90], [100, 110, 120]]]);QJsonArray coordsArray QJsonDocument::fromJson(coordsJson.toUtf8()).array();// 获取第二个点的Z坐标: [1][0][2]QJsonValue zCoord findInArrayByStringPath(coordsArray, 1.0.2);if (!zCoord.isUndefined()) {qDebug() Z坐标: zCoord.toInt(); // 输出: 90}3 树形结构使用数组表示// 使用数组表示的树形结构每个节点是一个数组 [value, [children...]]QString treeJson R([Root, [[A, [[A1, []],[A2, []]]],[B, [[B1, []],[B2, [[B2a, []]]]]]]]);QJsonArray tree QJsonDocument::fromJson(treeJson.toUtf8()).array();// 遍历树的深度优先搜索void traverseTree(const QJsonArray node, int depth 0) {if (node.size() 1) {QString indent(depth * 2, );qDebug() indent node[0].toString();if (node.size() 2 node[1].isArray()) {QJsonArray children node[1].toArray();for (const QJsonValue child : children) {if (child.isArray()) {traverseTree(child.toArray(), depth 1);}}}}}traverseTree(tree);五 通用嵌套数组操作函数1 检查路径是否有效bool isValidArrayPath(const QJsonArray array, const QVectorint path) {const QJsonArray* current array;for (int i 0; i path.size(); i) {int index path[i];if (index 0 || index current-size()) {return false; // 索引越界}// 如果是最后一个索引不需要检查类型if (i path.size() - 1) {return true;}// 中间路径必须是数组QJsonValue nextValue current-at(index);if (!nextValue.isArray()) {return false;}current nextValue.toArray();}return true;}2 设置嵌套数组的值/*** 设置嵌套数组中指定路径的值* 如果路径不存在会自动创建中间数组填充null值*/bool setNestedArrayValue(QJsonArray array, const QVectorint path, const QJsonValue value) {if (path.isEmpty()) {return false;}QJsonArray* current array;// 处理除最后一个索引外的所有路径for (int i 0; i path.size() - 1; i) {int index path[i];// 确保数组足够大while (current-size() index) {current-append(QJsonValue::Null);}// 如果当前位置不是数组用数组替换if (!current-at(index).isArray()) {(*current)[index] QJsonArray();}QJsonValue nextValue current-at(index);current const_castQJsonArray*(nextValue.toArray());}// 设置最后一个索引的值int lastIndex path.last();// 确保数组足够大while (current-size() lastIndex) {current-append(QJsonValue::Null);}(*current)[lastIndex] value;return true;}// 使用示例QJsonArray data;QVectorint path {0, 1, 2};setNestedArrayValue(data, path, Hello);// 现在 data[0][1][2] Hello自动创建了中间数组3 查找所有叶节点/*** 获取纯数组结构中的所有叶节点非数组的值*/QVectorQJsonValue getAllLeaves(const QJsonArray array) {QVectorQJsonValue leaves;for (const QJsonValue value : array) {if (value.isArray()) {leaves.append(getAllLeaves(value.toArray()));} else {leaves.append(value);}}return leaves;}// 使用示例QVectorQJsonValue leaves getAllLeaves(rootArray);for (const QJsonValue leaf : leaves) {qDebug() 叶节点: leaf.toString();}六 性能优化建议1 批量操作减少递归深度// 对于大量数据的查找考虑使用迭代而非递归QJsonValue iterativeFind(const QJsonArray array, const QVectorint path) {const QJsonValue* current nullptr;const QJsonArray* currentArray array;for (int i 0; i path.size(); i) {int index path[i];if (index 0 || index currentArray-size()) {return QJsonValue::Undefined;}current currentArray-at(index);// 如果是最后一个索引直接返回if (i path.size() - 1) {return *current;}// 否则继续深入if (current-isArray()) {currentArray current-toArray();} else {return QJsonValue::Undefined;}}return QJsonValue::Undefined;}2 缓存常用路径结果class CachedArrayLookup {private:QJsonArray data;QHashQString, QJsonValue cache; // 路径 - 值 的缓存public:CachedArrayLookup(const QJsonArray array) : data(array) {}QJsonValue get(const QString path) {if (cache.contains(path)) {return cache[path];}QJsonValue result findInArrayByStringPath(data, path);cache[path] result;return result;}void clearCache() {cache.clear();}};六 总结对于纯数组嵌套的JSONQJsonArray完全支持嵌套查找主要通过:1 整数索引路径逐层访问嵌套数组。2 辅助函数简化复杂路径的查找操作。3 类型检查确保每层都是数组类型。4 边界检查防止索引越界。虽然纯数组结构没有对象键名直观但通过良好的索引管理和辅助工具完全可以高效地进行嵌套查找和操作。在处理这类数据时建议1 封装查找函数以提高代码复用性。2 添加充分的错误检查和日志。3 对于复杂业务逻辑考虑添加索引映射层。4 注意性能避免不必要的深层递归。纯数组嵌套在某些场景如数学计算、游戏地图、多维数据中非常有用QJsonArray提供了足够的基础功能来处理这些需求。