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

如何在C#中拆分JSON数组的键值对并提取指定键的集合?

在C#中拆分JSON数组提取指定字段集合的实现方案

这事儿其实挺简单的,用C#里的JSON处理库配合Linq就能轻松搞定。我给你几种常用的实现方式,你可以根据自己的项目环境来选:

首先,先明确需求和注意点

你数据库里的JSON数组是:

[{"SelFoodId":"2","SelQuantity":"5"}, {"SelFoodId":"7","SelQuantity":"3"}, {"SelFoodId":"9","SelQuantity":"7"}]

需要转换成:

SelFoodId = {2,7}, SelQuantity = {5,7}

这里注意下示例结果和原JSON的对应关系有点小矛盾(原数组第三个元素的SelFoodId是9,但示例里没包含;SelQuantity取了第一个和第三个的5、7),后面我会在代码里标注怎么调整筛选逻辑。


方法一:使用Newtonsoft.Json(Json.NET)

这是.NET生态里老牌的JSON处理库,兼容性好,很多项目都在用。如果你的项目还没装,需要先通过NuGet安装Newtonsoft.Json包。

步骤1:定义对应实体类

先创建一个和JSON结构匹配的实体类,方便反序列化:

public class SelectedFood
{
    public string SelFoodId { get; set; }
    public string SelQuantity { get; set; }
    // 如果你的字段实际是数字类型,可以改成int,后续就不用转类型了
    // public int SelFoodId { get; set; }
    // public int SelQuantity { get; set; }
}

步骤2:解析JSON并提取字段

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

// 从数据库获取的JSON字符串
string jsonFromDb = @"[{""SelFoodId"":""2"",""SelQuantity"":""5""}, {""SelFoodId"":""7"",""SelQuantity"":""3""}, {""SelFoodId"":""9"",""SelQuantity"":""7""}]";

// 反序列化为实体列表
List<SelectedFood> foodList = JsonConvert.DeserializeObject<List<SelectedFood>>(jsonFromDb);

// 提取所有字段(如果要和示例结果一致,看下面的注释)
var selFoodIds = foodList.Select(f => f.SelFoodId).ToArray();
var selQuantities = foodList.Select(f => f.SelQuantity).ToArray();

// 如果你需要和示例结果完全一致(SelFoodId取2、7,SelQuantity取5、7),可以这样筛选:
// var selFoodIds = foodList.Take(2).Select(f => f.SelFoodId).ToArray(); // 取前两个的Id
// var selQuantities = foodList.Where((f, idx) => idx != 1).Select(f => f.SelQuantity).ToArray(); // 跳过第二个元素的Quantity

// 拼接成目标格式的字符串
string result = $"SelFoodId = {{{string.Join(",", selFoodIds)}}}, SelQuantity = {{{string.Join(",", selQuantities)}}}";

// 输出结果
Console.WriteLine(result);

方法二:使用System.Text.Json(.NET Core 3.0+自带)

如果你用的是.NET Core 3.0及以上版本,或者.NET 5+,可以直接用官方自带的System.Text.Json库,不需要额外安装包。

步骤1:同样定义实体类

和上面的SelectedFood类一样,这里就不重复写了。

步骤2:解析JSON并提取字段

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

string jsonFromDb = @"[{""SelFoodId"":""2"",""SelQuantity"":""5""}, {""SelFoodId"":""7"",""SelQuantity"":""3""}, {""SelFoodId"":""9"",""SelQuantity"":""7""}]";

// 反序列化为实体列表
List<SelectedFood> foodList = JsonSerializer.Deserialize<List<SelectedFood>>(jsonFromDb);

// 提取字段(同样,需要筛选的话参考上面的注释)
var selFoodIds = foodList.Select(f => f.SelFoodId).ToArray();
var selQuantities = foodList.Select(f => f.SelQuantity).ToArray();

// 拼接字符串
string result = $"SelFoodId = {{{string.Join(",", selFoodIds)}}}, SelQuantity = {{{string.Join(",", selQuantities)}}}";

Console.WriteLine(result);

额外技巧:不定义实体类直接处理

如果不想创建实体类,也可以直接解析JSON数组来提取字段,比如用Newtonsoft.Json的JArray:

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

string jsonFromDb = @"[{""SelFoodId"":""2"",""SelQuantity"":""5""}, {""SelFoodId"":""7"",""SelQuantity"":""3""}, {""SelFoodId"":""9"",""SelQuantity"":""7""}]";

// 解析为JArray
JArray foodArray = JArray.Parse(jsonFromDb);

// 提取字段
var selFoodIds = foodArray.Select(item => item["SelFoodId"].ToString()).ToArray();
var selQuantities = foodArray.Select(item => item["SelQuantity"].ToString()).ToArray();

// 拼接结果
string result = $"SelFoodId = {{{string.Join(",", selFoodIds)}}}, SelQuantity = {{{string.Join(",", selQuantities)}}}";

Console.WriteLine(result);

用System.Text.Json的话,可以用JsonElement来处理:

using System.Text.Json;
using System.Linq;

string jsonFromDb = @"[{""SelFoodId"":""2"",""SelQuantity"":""5""}, {""SelFoodId"":""7"",""SelQuantity"":""3""}, {""SelFoodId"":""9"",""SelQuantity"":""7""}]";

// 解析为JsonDocument
using JsonDocument doc = JsonDocument.Parse(jsonFromDb);

// 提取字段
var selFoodIds = doc.RootElement.EnumerateArray()
                    .Select(item => item.GetProperty("SelFoodId").GetString())
                    .ToArray();
var selQuantities = doc.RootElement.EnumerateArray()
                       .Select(item => item.GetProperty("SelQuantity").GetString())
                       .ToArray();

string result = $"SelFoodId = {{{string.Join(",", selFoodIds)}}}, SelQuantity = {{{string.Join(",", selQuantities)}}}";

Console.WriteLine(result);

内容的提问来源于stack exchange,提问作者Varinder Singh Baidwan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 03:58:49