gpt4 book ai didi

c# - 如何从 JsonDocument 中提取具有指定名称的所有 JsonProperty 对象的所有值?

转载 作者:行者123 更新时间:2023-12-04 10:18:00 26 4
gpt4 key购买 nike

关闭。这个问题需要更多focused .它目前不接受答案。












想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post .

去年关闭。




Improve this question




我有一个自由格式 JsonDocument 其中包含一个 JsonProperty与姓名 internalName .我的问题是我没有模型来解析它(因为没有预定义的 JSON 模式)并且该属性可以出现在任何级别,父级、子级等。

如何获得所有 JsonElement JSON 属性的值 internalName ?

到目前为止我尝试过的是这样的

    var namesUsedInLayout = Layout.RootElement.EnumerateObject().Where(x => x.Name == "internalName").Select(x => x.Value.ToString());

这虽然似乎只为我提供顶层,而不是子层或子层的子层。

最佳答案

您有一些已加载到 JsonElement 中的 JSON (或 JsonDocument )并希望递归查找给定属性名称的所有属性值。截至 JsonElement没有与 DescendantsAndSelf() 等效的方法也不支持 JSONPath queries ,因此您需要编写自己的递归算法。以下是其中之一:

public static partial class JsonExtensions
{
public static IEnumerable<JsonElement> DescendantPropertyValues(this JsonElement element, string name, StringComparison comparison = StringComparison.Ordinal)
{
if (name == null)
throw new ArgumentNullException();
return DescendantPropertyValues(element, n => name.Equals(n, comparison));
}

public static IEnumerable<JsonElement> DescendantPropertyValues(this JsonElement element, Predicate<string> match)
{
if (match == null)
throw new ArgumentNullException();
var query = RecursiveEnumerableExtensions.Traverse(
(Name: (string)null, Value: element),
t =>
{
switch (t.Value.ValueKind)
{
case JsonValueKind.Array:
return t.Value.EnumerateArray().Select(i => ((string)null, i));
case JsonValueKind.Object:
return t.Value.EnumerateObject().Select(p => (p.Name, p.Value));
default:
return Enumerable.Empty<(string, JsonElement)>();
}
}, false)
.Where(t => t.Name != null && match(t.Name))
.Select(t => t.Value);
return query;
}
}

public static partial class RecursiveEnumerableExtensions
{
// Rewritten from the answer by Eric Lippert https://stackoverflow.com/users/88656/eric-lippert
// to "Efficient graph traversal with LINQ - eliminating recursion" https://stackoverflow.com/questions/10253161/efficient-graph-traversal-with-linq-eliminating-recursion
// to ensure items are returned in the order they are encountered.
public static IEnumerable<T> Traverse<T>(
T root,
Func<T, IEnumerable<T>> children, bool includeSelf = true)
{
if (includeSelf)
yield return root;
var stack = new Stack<IEnumerator<T>>();
try
{
stack.Push(children(root).GetEnumerator());
while (stack.Count != 0)
{
var enumerator = stack.Peek();
if (!enumerator.MoveNext())
{
stack.Pop();
enumerator.Dispose();
}
else
{
yield return enumerator.Current;
stack.Push(children(enumerator.Current).GetEnumerator());
}
}
}
finally
{
foreach (var enumerator in stack)
enumerator.Dispose();
}
}
}

然后像这样使用它:
List<JsonElement> elements;
using (var doc = JsonDocument.Parse(jsonString))
{
elements = doc.RootElement.DescendantPropertyValues(internalName, comparison)
.Select(e => e.Clone()) // Clone the elements before disposing the JsonDocument
.ToList(); // Materialize the query before disposing the JsonDocument
}

笔记:
  • JsonDocument根据 docs 的说法,是一次性的,实际上必须被处理以避免内存泄漏。 .如果您需要搜索结果在文档的生命周期内继续存在,您必须clone元素并具体化查询。
  • 出于性能原因,我避免多次枚举每个对象,并使用显式堆栈而不是嵌套的递归枚举。

  • 演示 fiddle here .

    关于c# - 如何从 JsonDocument 中提取具有指定名称的所有 JsonProperty 对象的所有值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60994574/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com