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

如何在C#中使用Newtonsoft处理JSON动态键值对模型

在C#中用Newtonsoft解析并构建动态键值对JSON的方法

针对你这种键不固定的JSON结构,以下是几种实用的实现方式,全部基于Newtonsoft.Json(Json.NET)库:

一、解析动态键值对JSON

1. 使用JObject(灵活遍历、修改)

JObject是Newtonsoft.Json.Linq命名空间下的动态JSON对象,天生适配不确定键的场景:

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

// 示例JSON字符串
string json = @"{""product1"":""batch1,batch2"",""xyzproduct"":""batc265"",""productn"":""batch12""}";

// 解析为JObject
JObject productBatchObj = JObject.Parse(json);

// 遍历所有键值对
foreach (var prop in productBatchObj.Properties())
{
    string productKey = prop.Name;
    string batchValue = prop.Value.ToString();
    Console.WriteLine($"产品:{productKey},批次:{batchValue}");
}

// 动态获取指定键的值
if (productBatchObj.ContainsKey("xyzproduct"))
{
    string targetBatch = productBatchObj["xyzproduct"].ToString();
    Console.WriteLine($"xyzproduct对应的批次:{targetBatch}");
}

2. 反序列化为Dictionary<string, string>(强类型字典操作)

因为你的JSON结构是字符串键对应字符串值,直接反序列化为字典是最贴合的选择:

using Newtonsoft.Json;

string json = @"{""product1"":""batch1,batch2"",""xyzproduct"":""batc265"",""productn"":""batch12""}";

// 反序列化为字典
Dictionary<string, string> productBatchDict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);

// 遍历字典
foreach (var kvp in productBatchDict)
{
    Console.WriteLine($"产品:{kvp.Key},批次:{kvp.Value}");
}

// 安全获取指定键的值
if (productBatchDict.TryGetValue("productn", out string batch))
{
    Console.WriteLine($"productn对应的批次:{batch}");
}

3. 使用dynamic类型(简洁快速访问)

如果不需要编译时类型检查,dynamic可以让你直接通过属性或索引器访问JSON内容:

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

string json = @"{""product1"":""batch1,batch2"",""xyzproduct"":""batc265"",""productn"":""batch12""}";

// 反序列化为dynamic
dynamic productBatchDynamic = JsonConvert.DeserializeObject(json);

// 访问合法标识符格式的键
Console.WriteLine($"product1对应的批次:{productBatchDynamic.product1}");

// 访问带特殊字符的键(用索引器)
Console.WriteLine($"xyzproduct对应的批次:{productBatchDynamic["xyzproduct"]}");

// 遍历所有键值对(需转换为JObject)
foreach (var prop in (JObject)productBatchDynamic)
{
    Console.WriteLine($"产品:{prop.Key},批次:{prop.Value}");
}

二、构建动态键值对JSON

1. 用Dictionary<string, string>序列化

先构建字典,再序列化为JSON:

using Newtonsoft.Json;

Dictionary<string, string> productBatchDict = new Dictionary<string, string>();
productBatchDict.Add("productA", "batch3,batch4");
productBatchDict.Add("customProduct", "batch789");
productBatchDict.Add("productX", "batch001");

// 序列化为格式化后的JSON字符串
string json = JsonConvert.SerializeObject(productBatchDict, Formatting.Indented);
Console.WriteLine(json);

2. 用JObject构建并序列化

直接操作JObject添加键值对,再转为JSON:

using Newtonsoft.Json.Linq;

JObject productBatchObj = new JObject();
productBatchObj["productA"] = "batch3,batch4";
productBatchObj["customProduct"] = "batch789";
productBatchObj["productX"] = "batch001";

// 序列化为格式化后的JSON字符串
string json = productBatchObj.ToString(Formatting.Indented);
Console.WriteLine(json);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 02:18:06