记下fastjson的JSONPath属性判断语法变更,使用JSONPath过程中发现官方文档都还是旧的,导致按照官方文档写程序一直出错,看了源码才明白语法已变更。。。文档更新真是程序员的痛~
直接贴GitHub上开发者的测试案例
@Test
public void test0() {
assertEquals("{\"color\":\"red\",\"price\":19.95,\"gears\":[23,50],\"extra\":{\"x\":0},\"escape\":\"Esc\\b\\f\\n\\r\\t*\",\"nullValue\":null}"
, JSON.toJSONString(
JSONPath.extract(STR, "$.store.bicycle[?(@.color == 'red' )]"),
JSONWriter.Feature.WriteNulls
)
);
}
@Test
public void test1() {
assertEquals("{\"color\":\"red\",\"price\":19.95,\"gears\":[23,50],\"extra\":{\"x\":0},\"escape\":\"Esc\\b\\f\\n\\r\\t*\",\"nullValue\":null}"
, JSON.toJSONString(
JSONPath.extract(STR, "$.store.bicycle[?(@.gears == [23, 50])]"),
JSONWriter.Feature.WriteNulls
)
);
assertNull(JSONPath.extract(STR, "$.store.bicycle[?(@.gears == [23, 77])]"));
assertEquals("{\"color\":\"red\",\"price\":19.95,\"gears\":[23,50],\"extra\":{\"x\":0},\"escape\":\"Esc\\b\\f\\n\\r\\t*\",\"nullValue\":null}"
, JSON.toJSONString(
JSONPath.extract(STR, "$.store.bicycle[?(@.extra == {\"x\":0})]"),
JSONWriter.Feature.WriteNulls
)
);
assertEquals("{\"color\":\"red\",\"price\":19.95,\"gears\":[23,50],\"extra\":{\"x\":0},\"escape\":\"Esc\\b\\f\\n\\r\\t*\",\"nullValue\":null}"
, JSON.toJSONString(
JSONPath.extract(STR, "$.store.bicycle[?(@.escape == 'Esc\\b\\f\\n\\r\\t\\u002A')]"),
JSONWriter.Feature.WriteNulls
)
);
}
public final static String STR =
"{ \"store\": {\n" +
" \"book\": [ \n" +
" { \"category\": \"reference\",\n" +
" \"author\": \"Nigel Rees\",\n" +
" \"title\": \"Sayings of the Century\",\n" +
" \"price\": 8.95\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"Evelyn Waugh\",\n" +
" \"title\": \"Sword of Honour\",\n" +
" \"price\": 12.99\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"Herman Melville\",\n" +
" \"title\": \"Moby Dick\",\n" +
" \"isbn\": \"0-553-21311-3\",\n" +
" \"price\": 8.99\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"J. R. R. Tolkien\",\n" +
" \"title\": \"The Lord of the Rings\",\n" +
" \"isbn\": \"0-395-19395-8\",\n" +
" \"price\": 22.99\n" +
" }\n" +
" ],\n" +
" \"bicycle\": {\n" +
" \"color\": \"red\",\n" +
" \"price\": 19.95\n," +
" \"gears\": [23, 50]\n," +
" \"extra\": {\"x\": 0}\n," +
" \"escape\" : \"Esc\\b\\f\\n\\r\\t\\u002A\",\n" +
" \"nullValue\": null\n" +
" }\n" +
" }\n" +
"}";
本站更全的案例引用,之后才发现这篇浪费我不少时间
JAVA--基于阿里巴巴fastjson2 与 JsonPath以 jsonPath读取json内容示例及比较_java编程艺术的博客-CSDN博客_fastjson jsonpath