如何用C#解析含无命名节点的JSON,获取name值及对应根节点编号?
我来帮你解决这个C#解析无命名根节点JSON的问题,核心思路是把这个结构当成键为周期ID、值为周期对象的字典来处理,下面给你两种常用的实现方案:
先修正JSON语法错误
首先你的原始JSON有语法问题(多余的闭合括号和末尾逗号),修正后的正确格式如下:
{ "3925": { "totalExecutions": 2, "endDate": "", "description": "", "totalExecuted": 2, "started": "", "versionName": "Custom Pipes Development", "expand": "executionSummaries", "projectKey": "WUSDV007", "versionId": 22361, "environment": "", "totalCycleExecutions": 2, "totalDefects": 0, "build": "", "createdBy": "mghosh", "ended": "", "name": "SetMaxFutureDateFromCustomerField_Mobile", "totalFolders": 0, "modifiedBy": "mghosh", "projectId": 17101, "createdByDisplay": "Mayukh Ghosh", "startDate": "" }, "3932": { "totalExecutions": 2, "endDate": "", "description": "", "totalExecuted": 2, "started": "", "versionName": "Custom Pipes Development", "expand": "executionSummaries", "projectKey": "WUSDV007", "versionId": 22361, "environment": "", "totalCycleExecutions": 2, "totalDefects": 0, "build": "", "createdBy": "nkonda", "ended": "", "name": "WUSDV007-29779-Fee Validation", "totalFolders": 0, "modifiedBy": "nkonda", "projectId": 17101, "createdByDisplay": "Naveen Kumar Konda", "startDate": "" } }
方案1:使用Newtonsoft.Json(Json.NET)
这是.NET生态中广泛使用的JSON库,需要先通过NuGet安装Newtonsoft.Json包。
快速解析(无需实体类)
using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; class Program { static void Main() { string json = @"[上面修正后的JSON字符串]"; // 解析为字典,键就是根节点编号,值是对应的JSON对象 var cycleDict = JsonConvert.DeserializeObject<Dictionary<string, JObject>>(json); // 遍历获取每个编号和对应的name值 foreach (var kvp in cycleDict) { string cycleId = kvp.Key; string cycleName = kvp.Value["name"].ToString(); Console.WriteLine($"周期ID: {cycleId}, 名称: {cycleName}"); } } }
类型安全解析(使用实体类)
先定义对应JSON结构的实体类:
public class TestCycle { public int totalExecutions { get; set; } public string endDate { get; set; } public string description { get; set; } public int totalExecuted { get; set; } public string started { get; set; } public string versionName { get; set; } public string expand { get; set; } public string projectKey { get; set; } public int versionId { get; set; } public string environment { get; set; } public int totalCycleExecutions { get; set; } public int totalDefects { get; set; } public string build { get; set; } public string createdBy { get; set; } public string ended { get; set; } public string name { get; set; } public int totalFolders { get; set; } public string modifiedBy { get; set; } public int projectId { get; set; } public string createdByDisplay { get; set; } public string startDate { get; set; } }
然后解析为字典并遍历:
var cycleDict = JsonConvert.DeserializeObject<Dictionary<string, TestCycle>>(json); foreach (var kvp in cycleDict) { Console.WriteLine($"周期ID: {kvp.Key}, 名称: {kvp.Value.name}"); }
方案2:使用System.Text.Json(.NET Core 3.0+ / .NET 5+)
如果使用较新的.NET版本,可以直接用内置的System.Text.Json,无需额外安装包。
快速解析(无需实体类)
using System; using System.Collections.Generic; using System.Text.Json; class Program { static void Main() { string json = @"[上面修正后的JSON字符串]"; // 解析为字典,键是周期ID,值是JsonElement类型 var cycleDict = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(json); foreach (var kvp in cycleDict) { string cycleId = kvp.Key; string cycleName = kvp.Value.GetProperty("name").GetString(); Console.WriteLine($"周期ID: {cycleId}, 名称: {cycleName}"); } } }
类型安全解析(使用实体类)
同样使用上面定义的TestCycle类,解析代码如下:
var cycleDict = JsonSerializer.Deserialize<Dictionary<string, TestCycle>>(json); foreach (var kvp in cycleDict) { Console.WriteLine($"周期ID: {kvp.Key}, 名称: {kvp.Value.name}"); }
内容的提问来源于stack exchange,提问作者mach
相关产品推荐
相关产品推荐

