使用Newtonsoft JSON反序列化含特殊键的嵌套JSON并提取textCitations数据
C# 反序列化含特殊字符节点的JSON并提取textCitations数据
先贴出你提供的JSON示例:
{ "channelData":{ "pva:gpt-feedback":{ "summarizationOpenAIResponse":{ "result":{ "textCitations":[ { "id":"https://www.farmersdirect.com/", "text":"some text", "title":"Farmers Insurance", "type":"unknown", "entityType":"Claim", "entityContext":"https://schema.org", "url":"https://www.farmersdirect.com/" } ] } } } } }
作为C#和JSON新手,要提取textCitations数组里的字段值,核心是通过定义对应实体类来匹配JSON的层级结构,针对带冒号、连字符的特殊节点,用特性标注映射即可。下面是两种常用解决方案:
方案一:使用Newtonsoft.Json(Json.NET)
这是.NET生态中广泛使用的JSON处理库,需先通过NuGet安装Newtonsoft.Json包。
1. 定义匹配的实体类
根据JSON层级结构编写类,用[JsonProperty]特性映射特殊节点名称:
using Newtonsoft.Json; // 最外层根对象 public class RootObject { [JsonProperty("channelData")] public ChannelData ChannelData { get; set; } } public class ChannelData { // 映射带冒号和连字符的节点 [JsonProperty("pva:gpt-feedback")] public PvaGptFeedback PvaGptFeedback { get; set; } } public class PvaGptFeedback { [JsonProperty("summarizationOpenAIResponse")] public SummarizationOpenAIResponse SummarizationOpenAIResponse { get; set; } } public class SummarizationOpenAIResponse { [JsonProperty("result")] public Result Result { get; set; } } public class Result { [JsonProperty("textCitations")] public List<TextCitation> TextCitations { get; set; } } // textCitations数组中的单个对象 public class TextCitation { [JsonProperty("id")] public string Id { get; set; } [JsonProperty("text")] public string Text { get; set; } [JsonProperty("title")] public string Title { get; set; } [JsonProperty("type")] public string Type { get; set; } [JsonProperty("entityType")] public string EntityType { get; set; } [JsonProperty("entityContext")] public string EntityContext { get; set; } [JsonProperty("url")] public string Url { get; set; } }
2. 反序列化并提取数据
// 假设jsonString是你的JSON内容 string jsonString = "你的JSON文本内容"; // 反序列化为RootObject对象 RootObject root = JsonConvert.DeserializeObject<RootObject>(jsonString); // 提取textCitations数组 List<TextCitation> citations = root.ChannelData.PvaGptFeedback.SummarizationOpenAIResponse.Result.TextCitations; // 遍历数组获取每个字段值 foreach (var citation in citations) { Console.WriteLine($"ID: {citation.Id}"); Console.WriteLine($"Text: {citation.Text}"); Console.WriteLine($"Title: {citation.Title}"); // 其他字段可按需输出 }
方案二:使用System.Text.Json(.NET Core/.NET 5+内置)
如果使用.NET Core或.NET 5及以上版本,可直接用内置的System.Text.Json库,无需额外安装包。
1. 定义匹配的实体类
用[JsonPropertyName]特性映射特殊节点名称:
using System.Text.Json.Serialization; public class RootObject { [JsonPropertyName("channelData")] public ChannelData ChannelData { get; set; } } public class ChannelData { [JsonPropertyName("pva:gpt-feedback")] public PvaGptFeedback PvaGptFeedback { get; set; } } public class PvaGptFeedback { [JsonPropertyName("summarizationOpenAIResponse")] public SummarizationOpenAIResponse SummarizationOpenAIResponse { get; set; } } public class SummarizationOpenAIResponse { [JsonPropertyName("result")] public Result Result { get; set; } } public class Result { [JsonPropertyName("textCitations")] public List<TextCitation> TextCitations { get; set; } } public class TextCitation { [JsonPropertyName("id")] public string Id { get; set; } [JsonPropertyName("text")] public string Text { get; set; } [JsonPropertyName("title")] public string Title { get; set; } [JsonPropertyName("type")] public string Type { get; set; } [JsonPropertyName("entityType")] public string EntityType { get; set; } [JsonPropertyName("entityContext")] public string EntityContext { get; set; } [JsonPropertyName("url")] public string Url { get; set; } }
2. 反序列化并提取数据
string jsonString = "你的JSON文本内容"; RootObject root = System.Text.Json.JsonSerializer.Deserialize<RootObject>(jsonString); List<TextCitation> citations = root.ChannelData.PvaGptFeedback.SummarizationOpenAIResponse.Result.TextCitations; // 遍历输出示例 foreach (var citation in citations) { Console.WriteLine($"ID: {citation.Id}"); Console.WriteLine($"URL: {citation.Url}"); }
内容的提问来源于stack exchange,提问作者VishalTest
相关产品推荐
相关产品推荐

