擅长:python、mysql、java
<p>这就是你想做的吗?</p>
<pre><code>import objectpath
data = {
"user": {
"actions": {
"name": "reading",
"description": "blablabla"
},
"name": "John"
}
}
tree = objectpath.Tree(data)
result = tree.execute("$.user[@.name is 'John'].actions[@.name is 'reading'].description")
for entry in result:
print entry
</code></pre>
<p><strong>输出</strong></p>
<pre><code>blablabla
</code></pre>
<p>我必须修复你的JSON。另外,<code>tree.execute</code>返回一个生成器。可以用<code>print result.next()</code>替换<code>for</code>循环,但是<code>for</code>循环看起来更清晰。</p>