简而言之,我试图使用带有java的jsonpath从JSON数据中仅提取根节点。在下面的JSON数据中,根级别只有2个节点store
和expensive
。我的目标是获取存储在变量中的那两个节点的名称,然后继续创建一个断言,表示节点不为空或为空。
{
"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
这就是我的尝试:
JsonPath jpath = new JsonPath(json);
Map<String, Object> loc = jpath.get("$");
$告诉我所有的根节点和孩子们。有没有办法排除孩子并获得根节点?