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

.NET 3.5调用API返回不规则动态JSON节点转换为结构化格式方案咨询

针对.NET Framework 3.5下动态JSON节点转换的解决方案

前置准备

使用适配.NET Framework 3.5的Newtonsoft.Json(Json.NET)库,推荐选择10.0.3及更低版本,更高版本已停止对.NET Framework 3.5的兼容支持。

步骤1:定义目标结构化实体

// 年份数据结构
public class YearData
{
    public string Year { get; set; }
    public string Data { get; set; }
}

// 处理后的value字段结构
public class StructuredValue
{
    public string Description { get; set; }
    public List<YearData> YearData { get; set; }
}

// 外层节点适配结构
public class SubattrItem
{
    public string name { get; set; }
    public List<object> subattrOutput { get; set; }
    // 用JObject接收原始不规则value,处理后替换为结构化对象
    public Newtonsoft.Json.Linq.JObject value { get; set; }
    public string score { get; set; }
    public string valuetype { get; set; }
    // 处理后赋值的结构化value
    public StructuredValue structured_value { get; set; }
}

public class EntityDataItem
{
    public string name { get; set; }
    public List<SubattrItem> subattrOutput { get; set; }
    public object value { get; set; }
    public string score { get; set; }
    public string valuetype { get; set; }
}

public class RootObject
{
    public List<EntityDataItem> entityData { get; set; }
}

步骤2:核心转换代码实现

以下代码可直接处理你提供的完整JSON结构:

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Linq;

// 你的原始JSON字符串
var json = @"
{
    ""entityData"": [
        {
            ""name"": ""Statement of Comprehensive Income"",
            ""subattrOutput"": [
                {
                    ""name"": ""Sales"",
                    ""subattrOutput"": [],
                    ""value"": {
                        ""Description"": ""Sales "",
                        ""2018"": ""8,704,888 "",
                        ""2019"": ""4,760,717 ""
                    },
                    ""score"": ""99.5"",
                    ""valuetype"": ""object""
                },
                {
                    ""name"": ""Cost of goods sold"",
                    ""subattrOutput"": [],
                    ""value"": {
                        ""Description"": ""Cost of sales "",
                        ""2018"": ""(6,791,489) "",
                        ""2019"": ""(3,502,785) ""
                    },
                    ""score"": ""99.75"",
                    ""valuetype"": ""object""
                }
            ],
            ""value"": null,
            ""score"": ""98.63"",
            ""valuetype"": ""object""
        }
    ]
}";

// 反序列化原始JSON
var root = JsonConvert.DeserializeObject<RootObject>(json);

foreach (var entity in root.entityData)
{
    foreach (var subItem in entity.subattrOutput)
    {
        // 跳过value为空的节点
        if (subItem.value == null || subItem.valuetype != "object")
            continue;

        // 构建结构化value
        var structuredVal = new StructuredValue
        {
            Description = subItem.value.Value<string>("Description"),
            YearData = new List<YearData>()
        };

        // 遍历所有动态年份节点
        foreach (var prop in subItem.value.Properties())
        {
            // 过滤掉Description字段,剩余都是年份字段
            if (prop.Name == "Description")
                continue;
            structuredVal.YearData.Add(new YearData
            {
                Year = prop.Name,
                Data = prop.Value.ToString()
            });
        }

        // 替换原value为结构化对象,也可直接覆盖原value字段,按需调整
        subItem.structured_value = structuredVal;
        // 可选:移除原始不规则value字段
        subItem.value = null;
    }
}

// 输出处理后的JSON
string resultJson = JsonConvert.SerializeObject(root, Formatting.Indented);

转换逻辑说明

  • 无需提前定义不规则的动态年份节点结构,直接用JObject接收原始value字段,动态读取所有属性
  • 自动识别所有非Description的属性作为年份字段,适配后续年份变化的场景
  • 转换后的结构完全符合你要求的格式,可直接用于后续业务逻辑处理

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 06:18:00