You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用System.Text.Json序列化double[,]二维数组至JSON?含数组转换及文件保存方案

确实,System.Text.Json默认不支持直接序列化多维矩形数组(比如double[,]这种),我给你两个实用的解决方案,你可以根据自己的需求选择:

方案一:转换为List<List<double>>再序列化

这是最直接的办法——把二维数组转换成嵌套的列表结构,而System.Text.Json对List<T>的序列化支持非常好。

示例代码如下:

using System.Text.Json;
using System.IO;

// 你的原始二维数组
double[,] originalArray = new double[,]
{
    { 1.1, 2.2, 3.3 },
    { 4.4, 5.5, 6.6 },
    { 7.7, 8.8, 9.9 }
};

// 转换为 List<List<double>>
var nestedList = new List<List<double>>();
int rowCount = originalArray.GetLength(0); // 获取行数
int colCount = originalArray.GetLength(1); // 获取列数

for (int row = 0; row < rowCount; row++)
{
    var currentRow = new List<double>();
    for (int col = 0; col < colCount; col++)
    {
        currentRow.Add(originalArray[row, col]);
    }
    nestedList.Add(currentRow);
}

// 序列化并写入文件
string jsonContent = JsonSerializer.Serialize(nestedList);
File.WriteAllText("array_output.json", jsonContent);

这段代码的逻辑很清晰:通过GetLength(0)和GetLength(1)分别拿到二维数组的行数和列数,然后逐行遍历,把每一行的元素存入一个List<double>,最后把所有行列表加入外层的List<List<double>>,就能直接被System.Text.Json序列化了。

方案二:自定义JsonConverter直接处理double[,]

如果你不想修改原有的数据结构,希望直接序列化double[,],可以自定义一个JSON转换器来告诉System.Text.Json怎么处理这种类型。

首先定义转换器类:

using System.Text.Json;
using System.Text.Json.Serialization;

public class Double2DArrayConverter : JsonConverter<double[,]>
{
    // 序列化逻辑:把二维数组写成嵌套的JSON数组
    public override void Write(Utf8JsonWriter writer, double[,] value, JsonSerializerOptions options)
    {
        writer.WriteStartArray();
        int rowCount = value.GetLength(0);
        int colCount = value.GetLength(1);

        for (int row = 0; row < rowCount; row++)
        {
            writer.WriteStartArray();
            for (int col = 0; col < colCount; col++)
            {
                writer.WriteNumberValue(value[row, col]);
            }
            writer.WriteEndArray();
        }
        writer.WriteEndArray();
    }

    // 反序列化逻辑(如果需要从JSON还原回double[,]的话)
    public override double[,] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        // 先把JSON数组反序列化为List<List<double>>
        var nestedList = JsonSerializer.Deserialize<List<List<double>>>(ref reader, options);
        if (nestedList == null || nestedList.Count == 0)
            return new double[0, 0];

        int rowCount = nestedList.Count;
        int colCount = nestedList[0].Count;
        var resultArray = new double[rowCount, colCount];

        for (int row = 0; row < rowCount; row++)
        {
            for (int col = 0; col < colCount; col++)
            {
                resultArray[row, col] = nestedList[row][col];
            }
        }
        return resultArray;
    }
}

然后使用这个转换器来序列化你的数组:

using System.Text.Json;
using System.IO;

double[,] originalArray = new double[,]
{
    { 1.1, 2.2, 3.3 },
    { 4.4, 5.5, 6.6 }
};

// 创建序列化选项并添加自定义转换器
var serializeOptions = new JsonSerializerOptions();
serializeOptions.Converters.Add(new Double2DArrayConverter());

// 序列化并写入文件
string jsonContent = JsonSerializer.Serialize(originalArray, serializeOptions);
File.WriteAllText("array_output.json", jsonContent);

这个方案的好处是你不需要改动原有的数据结构,直接用double[,]进行序列化和反序列化,转换器会帮你处理和JSON格式的映射。

两种方案的选择建议

  • 如果之后的业务逻辑更适合用List<List<double>>来处理数据,优先选方案一;
  • 如果需要保留原有的double[,]结构,不想做额外的转换操作,就用方案二。

内容的提问来源于stack exchange,提问作者Fat_Doge

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 07:32:46