Solon v3.6.2

snack - JsonPath 应用参考

</> markdown

1、JsonPath 应用参考

应用接口描述备注
ONode:select(...) -> ONode查找
ONode:exists(...) -> bool包括
ONode:delete(...)删除
ONode:create(...)创建表达式不能有 ...x* 片段(因为自己是空,没法展开)

应用示例

ONode oNode = ONode.ofBean(store);

oNode.select("$..book[?@.tags contains 'war'].first()").toBean(Book.class); //RFC9535 规范,可以没有括号
oNode.select("$..book[?(!(@.category == 'fiction') && @.price < 40)].first()").toBean(Book.class);
oNode.select("$.store.book.count()");

ONode.ofJson(store).create("$.store.book[0].category").toJson();

ONode.ofBean(store).delete("$..book[-1]");

2、表达式示例与说明

样本数据

{ "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": 399
    }
  }
}

示例JSONPath表达式及其应用于示例JSON值时的预期结果

JSONPath预期结果
$.store.book[*].author书店里所有书的作者
$..autho所有作者
$.store.*商店里的所有东西,包括一些书和一辆红色的自行车
$.store..price商店里所有东西的价格
$..book[2]第三本书
$..book[2].author第三本书的作者
$..book[2].publisher空结果:第三本书没有“publisher”成员
$..book[-1]最后一本书
$..book[0,1]
$..book[:2]
前两本书
$..book[?@.isbn]所有有国际标准书号的书
$..book[?@.price<10]所有比10便宜的书
$..*输入值中包含的所有成员值和数组元素