我有一个嵌套的json结构,我使用的是objectpath(python API版本),但是我不知道如何选择和过滤一些信息(更确切地说是结构中的嵌套信息)。
例如。 我想为用户“John”选择操作“reading”的“description”。
JSON格式:
{
"user":
{
"actions":
[
{
"name": "reading",
"description": "blablabla"
}
]
"name": "John"
}
}
代码:
$.user[@.name is 'John' and @.actions.name is 'reading'].actions.description
但它不起作用(空集合,但在我的JSON中不是这样)。 有什么建议吗?
这就是你想做的吗?
输出
我必须修复你的JSON。另外,
tree.execute
返回一个生成器。可以用print result.next()
替换for
循环,但是for
循环看起来更清晰。我相信你在JSON中遗漏了一个逗号:
假设只有一个“John”,只有一个“reading”活动,则以下查询有效:
如果可能有多个“John”s和多个“reading”活动,则以下查询几乎可以工作:
我说几乎是,因为如果有其他嵌套字典包含“name”和“description”条目,比如
要获得正确的查询,需要正确地将查询实现到数组中,https://github.com/adriank/ObjectPath/issues/60
相关问题 更多 >
编程相关推荐