fastjson的JSONPath语法变更

记下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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值