1

我正在尝试使用 $match 运算符和谓词中的正则表达式来使用 jsonata4java。

jsonata 代码已在 JSonata 练习器应用程序上成功测试,但在我的 java 测试中失败,错误消息似乎来自 antlr(词法分析器/解析器)。锻炼器与相同的输入一起使用。

尝试的事情:1)升级到最新版本的jsonata4java(1.0.5)。2) 转义某些字符的各种方法(在黑暗中拍摄) 3) 简化表达式 4) 在谓词中使用 lambda 样式 (tin ~> /regex/) 而不是匹配 5) 逐步执行一些生成的解析代码

    public void test() {
        try {
            Expressions expr = null;
            ObjectMapper mapper = new ObjectMapper();
                JsonNode jsonObj = null;
                String json = "{\"id\":\"1234\"}";
                String expression = "$$[$match(id, /1234/)].id";
//              String expression = "$$.id";
                System.out.println("Expression: " + expression);
                jsonObj = mapper.readTree(json);
                expr = Expressions.parse(expression);
                JsonNode jsonResult = expr.evaluate(jsonObj);
                System.out.println("" + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonResult));
        }
        catch(Throwable th) {
            System.out.println(th.getMessage());
        }
    }

我希望这段代码只显示“id”属性“1234”。相反,它会在执行 Expressions.parse(expression) 时引发异常。错误消息的文本是:

Expression: $$[$match(id, /1234/)].id

line 1:2 at [@1,2:2='[',<16>,1:2]: missing '.' at '[',
line 1:14 at [@6,14:14='/',<29>,1:14]: no viable alternative at input '$match(id,/', 
line 1:14 at [@6,14:14='/',<29>,1:14]: extraneous input '/' expecting {'(', 
'true', 'false', STRING, 'null', '[', '{', '$', '$$', NUMBER, 'function', '-', '$each', '$sift', '$reduce', '$filter', '$map', VAR_ID, ID}, line 1:20 at [@9,20:20=')',<4>,1:20]: no viable alternative at input '1234/)'

嘿嘿,好久没发帖了,多谢帮助。

4

0 回答 0