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

C#如何序列化JSON对象数组并将对象名转为属性值

如何将JSON对象键转为.NET类的属性值

针对把JSON中letterFields的对象名(如Name)序列化为DocumentField的FieldName属性的需求,提供两种可行方案:

方案一:修改属性类型为字典,再转换为列表

直接调整DocumentFieldMap的属性类型,用字典接收JSON中的键值对,再通过只读属性转换为所需列表:

修改后的C#类

public class DocumentFieldMap
{
    [Key]
    [JsonPropertyName("letterCode")]
    public string DocumentCode { get; set; }
    
    [JsonPropertyName("letterFields")]
    public Dictionary<string, DocumentField> DocumentFieldsDict { get; set; }
    
    // 自动将字典转为目标列表的只读属性
    public List<DocumentField> DocumentFields => DocumentFieldsDict?
        .Select(kv => new DocumentField 
        { 
            FieldName = kv.Key, 
            DataSource = kv.Value.DataSource, 
            ReadOnly = kv.Value.ReadOnly 
        })
        .ToList();
}

public class DocumentField
{
    [Key]
    public string FieldName { get; set; }
    public string DataSource { get; set; }
    public string ReadOnly { get; set; }
}

使用示例

反序列化后直接通过DocumentFields获取转换好的列表:

var json = "你的JSON字符串";
var result = JsonSerializer.Deserialize<List<DocumentFieldMap>>(json);
var targetFieldsList = result.First().DocumentFields;

方案二:自定义JsonConverter处理列表转换

如果不想修改原类结构,可通过自定义转换器实现JSON对象到List<DocumentField>的自动转换:

自定义转换器代码

public class DocumentFieldListConverter : JsonConverter<List<DocumentField>>
{
    public override List<DocumentField> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        using var jsonDoc = JsonDocument.ParseValue(ref reader);
        var fieldsList = new List<DocumentField>();
        
        // 遍历JSON对象的每个键值对,把键赋值给FieldName
        foreach (var property in jsonDoc.RootElement.EnumerateObject())
        {
            var field = JsonSerializer.Deserialize<DocumentField>(property.Value.GetRawText(), options);
            field.FieldName = property.Name;
            fieldsList.Add(field);
        }
        
        return fieldsList;
    }

    public override void Write(Utf8JsonWriter writer, List<DocumentField> value, JsonSerializerOptions options)
    {
        // 如需序列化回JSON,实现列表转对象的逻辑
        writer.WriteStartObject();
        foreach (var field in value)
        {
            writer.WritePropertyName(field.FieldName);
            JsonSerializer.Serialize(writer, new { field.DataSource, field.ReadOnly }, options);
        }
        writer.WriteEndObject();
    }
}

在原类中应用转换器

给DocumentFields属性添加转换器标记即可:

public class DocumentFieldMap
{
    [Key]
    [JsonPropertyName("letterCode")]
    public string DocumentCode { get; set; }
    
    [JsonPropertyName("letterFields")]
    [JsonConverter(typeof(DocumentFieldListConverter))]
    public List<DocumentField> DocumentFields { get; set; }
}

public class DocumentField
{
    [Key]
    public string FieldName { get; set; }
    public string DataSource { get; set; }
    public string ReadOnly { get; set; }
}

使用示例

直接反序列化,转换器会自动处理键到属性的映射:

var json = "你的JSON字符串";
var result = JsonSerializer.Deserialize<List<DocumentFieldMap>>(json);

两种方案各有优势:方案一实现简单,适合无需保留原列表序列化逻辑的场景;方案二维持原类结构不变,适配已有代码的兼容需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 12:33:41