如何在C#中将字符串格式的3D矩阵数据映射为3D数组?
C#解析动态尺寸3D矩阵字符串的简单方案
你这个矩阵字符串是标准的JSON嵌套数组格式,不用自己写复杂的字符遍历逻辑,直接用现成的JSON解析库就能轻松搞定,完全适配动态尺寸的需求,不用硬编码行/列数。下面给你两种可行的实现方式,都是新手友好型的:
第一步:定义矩阵单元的结构
每个单元是[整数, 整数列表],先写一个类来对应这个结构:
public class MatrixCell { public int Value { get; set; } public List<int> RelatedIds { get; set; } = new List<int>(); }
方案一:用Json.NET(推荐,灵活易上手)
这个是第三方库,需要先在NuGet里安装Newtonsoft.Json:在Visual Studio里右键项目→管理NuGet包,搜索安装即可。
然后写解析代码:
using Newtonsoft.Json.Linq; using System.Collections.Generic; // 假设matrixString是你从文件里读出来的第一行字符串 string matrixString = "[[[[1, []], [6, [1, 2, 11]], [0, [11]]], [[0, [2]], [0, [2]], [0, [11]]], [[0, [1]], [6, []], [0, [11]]]], [[[0, []], [0, []], [0, [11]]], [[0, []], [0, []], [0, [11]]], [[0, [2]], [0, [1,2]], [0, [11]]]], [[[0, [2]], [6, [11]], [0, [11]]], [[0, [6, 4]], [0, []], [0, [11]]], [[0, [1, 2]], [6, []], [0, [2, 11]]]]]"; // 把字符串解析成最外层的数组(对应3个层) JArray layers = JArray.Parse(matrixString); // 初始化3D列表:层 → 行 → 列 → MatrixCell List<List<List<MatrixCell>>> matrix3D = new List<List<List<MatrixCell>>>(); foreach (JArray layer in layers) { List<List<MatrixCell>> currentLayer = new List<List<MatrixCell>>(); foreach (JArray row in layer) { List<MatrixCell> currentRow = new List<MatrixCell>(); foreach (JArray cell in row) { // 解析单元的两个元素:第一个是整数,第二个是整数列表 int value = (int)cell[0]; List<int> relatedIds = cell[1].ToObject<List<int>>(); currentRow.Add(new MatrixCell { Value = value, RelatedIds = relatedIds }); } currentLayer.Add(currentRow); } matrix3D.Add(currentLayer); }
方案二:用.NET原生的System.Text.Json(无需额外安装库)
如果不想用第三方库,.NET Core 3.0+或者.NET 5+自带的System.Text.Json也能搞定:
using System.Text.Json; using System.Collections.Generic; // 同样的matrixString string matrixString = "[[[[1, []], [6, [1, 2, 11]], [0, [11]]], [[0, [2]], [0, [2]], [0, [11]]], [[0, [1]], [6, []], [0, [11]]]], [[[0, []], [0, []], [0, [11]]], [[0, []], [0, []], [0, [11]]], [[0, [2]], [0, [1,2]], [0, [11]]]], [[[0, [2]], [6, [11]], [0, [11]]], [[0, [6, 4]], [0, []], [0, [11]]], [[0, [1, 2]], [6, []], [0, [2, 11]]]]]"; // 先反序列化为JsonElement,因为尺寸是动态的 JsonElement root = JsonSerializer.Deserialize<JsonElement>(matrixString); List<List<List<MatrixCell>>> matrix3D = new List<List<List<MatrixCell>>>(); foreach (JsonElement layerElement in root.EnumerateArray()) { List<List<MatrixCell>> currentLayer = new List<List<MatrixCell>>(); foreach (JsonElement rowElement in layerElement.EnumerateArray()) { List<MatrixCell> currentRow = new List<MatrixCell>(); foreach (JsonElement cellElement in rowElement.EnumerateArray()) { // 解析单元的两个元素 int value = cellElement[0].GetInt32(); List<int> relatedIds = new List<int>(); foreach (JsonElement idElement in cellElement[1].EnumerateArray()) { relatedIds.Add(idElement.GetInt32()); } currentRow.Add(new MatrixCell { Value = value, RelatedIds = relatedIds }); } currentLayer.Add(currentRow); } matrix3D.Add(currentLayer); }
验证解析结果
比如要访问你说的左上角(x1,y1,z1)对应的单元,就是matrix3D[0][0][0]:
MatrixCell firstCell = matrix3D[0][0][0]; Console.WriteLine($"值:{firstCell.Value},关联ID:{string.Join(", ", firstCell.RelatedIds)}"); // 输出:值:1,关联ID:(因为是空列表)
关键说明
- 代码完全是动态遍历的,不管行/列数怎么变,层数量是不是3,都能自动适配,不用硬编码任何尺寸数值。
- 对于72×43×3这种大矩阵,两种方案的性能都足够,不会有明显卡顿。
内容的提问来源于stack exchange,提问作者MACMAN2003
相关产品推荐
相关产品推荐

