当您不知道是否正在处理对象或数组时使用JsonPath

时间:2016-11-05 00:12:28

标签: json jsonpath

给出以下JSON:

id  name  fullname            parent_id
--  ----  -----------------   ---------
1   cat1  cat0 > cat1         null
2   cat2  cat0 > cat1 > cat2  1
3   cat3  cat1 > cat3         1 

我们可以使用类似{ "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": { "color": "red", "price": 19.95 } }, "expensive": 10 } 的路径表达式来获取所有作者。但是如果我们事先不知道$.store.book[*].author是数组还是对象,例如如果我们可以拥有上述JSON,但也会这样:

book

是否有“通用路径”可用于在任何一种情况下获取所有作者?

1 个答案:

答案 0 :(得分:1)

是的,您可以使用:

$.store..author

可测试:

https://jsonpath.curiousconcept.com/

'希望这会有所帮助。