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

基于NewtonSoft JSON解析合一JSON文件至DataTable的高效方案咨询

高效解析混合单属性与动态数组的JSON到DataTable(NewtonSoft JSON)

不用逐行检测Token,利用NewtonSoft的JObject和内置的DataTable反序列化能力就能高效实现需求。核心思路是一次性解析整个JSON为JObject,区分顶层的单值属性和数组属性,分别转换为对应DataTable,自动适配动态数组的存在性。

实现步骤与代码示例

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Data;
using System.Linq;

public class JsonDataTableConverter
{
    public Dictionary<string, DataTable> ConvertCombinedJson(string jsonContent)
    {
        var dataTables = new Dictionary<string, DataTable>();
        var rootObject = JObject.Parse(jsonContent);

        // 处理顶层单值属性,生成名为"TopLevelProperties"的DataTable
        var topLevelTable = new DataTable("TopLevelProperties");
        var singleValueProps = rootObject.Properties().Where(p => p.Value.Type != JTokenType.Array).ToList();
        
        // 添加列
        foreach (var prop in singleValueProps)
        {
            topLevelTable.Columns.Add(prop.Name, GetCorrespondingClrType(prop.Value.Type));
        }
        
        // 填充行数据
        var row = topLevelTable.NewRow();
        foreach (var prop in singleValueProps)
        {
            row[prop.Name] = prop.Value.ToObject(GetCorrespondingClrType(prop.Value.Type));
        }
        topLevelTable.Rows.Add(row);
        dataTables.Add(topLevelTable.TableName, topLevelTable);

        // 处理所有动态数组,每个数组生成对应名称的DataTable
        foreach (var arrayProp in rootObject.Properties().Where(p => p.Value.Type == JTokenType.Array))
        {
            var arrayTable = arrayProp.Value.ToObject<DataTable>();
            arrayTable.TableName = arrayProp.Name;
            dataTables.Add(arrayTable.TableName, arrayTable);
        }

        return dataTables;
    }

    // 辅助方法:映射JToken类型到CLR类型
    private Type GetCorrespondingClrType(JTokenType tokenType)
    {
        return tokenType switch
        {
            JTokenType.String => typeof(string),
            JTokenType.Integer => typeof(int),
            JTokenType.Float => typeof(double),
            JTokenType.Boolean => typeof(bool),
            JTokenType.Date => typeof(DateTime),
            JTokenType.Guid => typeof(Guid),
            _ => typeof(object)
        };
    }
}

关键说明

  1. 高效性:JObject.Parse一次性完成JSON解析,内部采用优化的解析逻辑,比逐行遍历Token更简洁高效;数组转DataTable直接用NewtonSoft内置的序列化逻辑,无需手动映射字段。
  2. 动态数组适配:遍历JObject时仅处理类型为JTokenType.Array的属性,自动忽略JSON中缺失的数组,无需提前依赖Schema定义。
  3. 顶层属性灵活处理:示例中把顶层单值属性整理为一行多列的DataTable,如果需要一行一个属性的格式,可将列改为PropertyName和PropertyValue,循环添加每行数据即可。

使用示例

// 读取JSON文件内容
var jsonContent = File.ReadAllText("combined.json");
var converter = new JsonDataTableConverter();
var result = converter.ConvertCombinedJson(jsonContent);

// 访问顶层属性表
var topLevelTable = result["TopLevelProperties"];
// 访问Array01对应的表
var array01Table = result["Array01"];

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 02:52:51