c# - 使用 System.text.json 选择部分 json
问题描述
我开始使用 c# 并反序列化 Json。在我的培训中,我学习了 Newtonsoft,但我想对 system.text.json 做同样的事情
有了这个json,我想选择
- 搜索产品 > 特色产品 AND
- 搜索产品 > 产品详情
制作对象列表。
https://api.nvidia.partners/edge/product/search?page=1&limit=9&locale=fr-fr&category=GPU&gpu=RTX%203090,RTX%203080%20Ti,RTX%203080,RTX%203070%20Ti,RTX%203070,RTX%203060%20Ti,RTX%203060&gpu_filter=RTX%203090~12,RTX%203080%20Ti~7,RTX%203080~16,RTX%203070%20Ti~3,RTX%203070~18,RTX%203060%20Ti~8,RTX%203060~2,RTX%202080%20SUPER~1,RTX%202080~0,RTX%202070%20SUPER~0,RTX%202070~0,RTX%202060~6,GTX%201660%20Ti~0,GTX%201660%20SUPER~9,GTX%201660~8,GTX%201650%20Ti~0,GTX%201650%20SUPER~3,GTX%201650~17
班级
public class CarteGraphique
{
public string displayName { get; set; }
public string prdStatus { get; set; }
public List<Retailer> retailers { get; set; }
}
使用 Newtonsoft,我执行以下操作:
牛顿软件
JObject jsonParse = JObject.Parse(json);
IList<CarteGraphique> products = new List<CarteGraphique>();
IList<JToken> productDetailsParse = jsonParse["searchedProducts"]["productDetails"]
.Children()
.Where(n => n["isFounderEdition"].Value<bool>() == true)
.ToList();
var featuredProductParse = jsonParse["searchedProducts"]["featuredProduct"];
foreach (JToken item in productDetailsParse)
{
CarteGraphique result = item.ToObject<CarteGraphique>();
products.Add(result);
}
var featuredProduct = featuredProductParse.ToObject<CarteGraphique>();
products.Add(featuredProduct);
foreach (var item in products)
{
Console.WriteLine(item.DisplayName);
}
我想用 System.Text.Json 做到这一点......但我不知道如何选择 json 部分“productDetails”将其添加到对象列表中。
System.text.json
var listGpu = new List<CarteGraphique>();
var jsonParse = JsonDocument.Parse(json);
var jsonFilter = jsonParse.RootElement
.GetProperty("searchedProducts")
.GetProperty("featuredProduct")
.GetRawText();
var jsonObj = JsonSerializer.Deserialize<CarteGraphique>(jsonFilter);
listGpu.Add(jsonObj);
foreach (var item in listGpu)
{
Console.WriteLine(item.displayName);
}
你能帮我吗?对于我这个初学者来说,文档不清楚。
解决方案
您可以模拟 Json.NET 逻辑,如下所示:
using var jsonParse = JsonDocument.Parse(json); // Be sure to dispose the JsonDocument!
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var products = jsonParse.RootElement
.GetProperty("searchedProducts") // Get the searchedProducts value
.GetProperty("productDetails") // Get the productDetails value
.EnumerateArray() // Enumerate its items
.Where(n => n.GetProperty("isFounderEdition").GetBoolean()) // Filter on those for which isFounderEdition == true
.Select(n => n.ToObject<CarteGraphique>(options)) // Deserialize to a CarteGraphique
.ToList();
// Add the searchedProducts.featuredProduct item to the list.
var featuredProduct = jsonParse.RootElement
.GetProperty("searchedProducts")
.GetProperty("featuredProduct")
.ToObject<CarteGraphique>(options);
products.Add(featuredProduct);
从这个答案到System.Text.Json.JsonElement ToObject 解决ToObject<T>(this JsonElement element, JsonSerializerOptions options = null)
方法的扩展方法在哪里:
public static partial class JsonExtensions
{
public static T ToObject<T>(this JsonElement element, JsonSerializerOptions options = null)
{
var bufferWriter = new System.Buffers.ArrayBufferWriter<byte>();
using (var writer = new Utf8JsonWriter(bufferWriter))
element.WriteTo(writer);
return JsonSerializer.Deserialize<T>(bufferWriter.WrittenSpan, options);
}
public static T ToObject<T>(this JsonDocument document, JsonSerializerOptions options = null)
{
if (document == null)
throw new ArgumentNullException(nameof(document));
return document.RootElement.ToObject<T>(options);
}
}
笔记:
JsonDocument
是一次性的。根据文档,未能处理它将导致内存无法返回到池中,这将增加对框架各个部分的 GC 影响。在 .NET 6.0 中,Microsoft 计划从 a 实现直接反序列化
JsonElement
,请参阅我们应该能够从 DOM #31274 进行序列化和反序列化。同时可以使用上述扩展方法。我没有添加任何对缺失属性的检查。如果可能缺少属性,请使用
JsonElement.TryGetProperty()
. 请参阅使用 System.Text.Json 获取嵌套属性,了解一些可以简化此操作的扩展方法。与 Json.NET 不同,System.Text.Json 默认区分大小写。要启用不区分大小写的反序列化,请使用JsonSerializer.Deserialize failed
JsonSerializerOptions { PropertyNameCaseInsensitive = true }
中所示的方法。
演示小提琴在这里。