使用 Newtonsoft JSON 从 JSON 数组中提取数组元素

kun*_*tal 0 c# json json.net

我有一个像下面这样的 JSON 数据

[{"Staffs":[5,10,12,14]},{"Staffs":[11,13,15,17]}]
Run Code Online (Sandbox Code Playgroud)

我想从中提取价值并期待以下数据

[5,10,12,14,11,13,15,17]
Run Code Online (Sandbox Code Playgroud)

我怎样才能用 newtonsoft JSON 做到这一点。

Swe*_*per 5

您可以先将 JSON 解析为JArray

using Newtonsoft.Json.Linq;

var array = JArray.Parse(yourJSONString);
Run Code Online (Sandbox Code Playgroud)

然后,使用SelectMany将其展平并将其转换为 a List<int>

var result = array.SelectMany(x => x["Staffs"]).Values<int>().ToList();
Run Code Online (Sandbox Code Playgroud)