Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

无法通过.访问list元素的属性 #565

Open
hhhyang opened this issue Aug 29, 2023 · 0 comments
Open

无法通过.访问list元素的属性 #565

hhhyang opened this issue Aug 29, 2023 · 0 comments

Comments

@hhhyang
Copy link

hhhyang commented Aug 29, 2023

我有一个代码片段:

String jsonStr = """
                {
                    "a": {
                        "b": [
                                {
                                    "x": 3
                                },
                                {
                                    "x": 4
                                }
                            ]
                    }
                }
                """;


  JSONObject jsonObj = new JSONObject(jsonStr);
  Object value = AviatorEvaluator.execute("a.b[0].x", jsonObj.toMap());

执行时会报错:

com.googlecode.aviator.exception.ExpressionSyntaxErrorException: Syntax error: unexpect token ']', maybe forget to insert ';' to complete last expression  at 5, lineNumber: 1, token : [type='Char',lexeme=']',index=5],
while parsing expression: `
a.b[0]^^^
`
	at com.googlecode.aviator.AviatorEvaluatorInstance.innerCompile(AviatorEvaluatorInstance.java:1545) ~[aviator-5.3.3.jar:na]
	at com.googlecode.aviator.AviatorEvaluatorInstance.compile(AviatorEvaluatorInstance.java:1506) ~[aviator-5.3.3.jar:na]
	at com.googlecode.aviator.AviatorEvaluatorInstance.compile(AviatorEvaluatorInstance.java:1464) ~[aviator-5.3.3.jar:na]
	at com.googlecode.aviator.AviatorEvaluatorInstance.compile(AviatorEvaluatorInstance.java:1451) ~[aviator-5.3.3.jar:na]
	at com.googlecode.aviator.AviatorEvaluatorInstance.execute(AviatorEvaluatorInstance.java:1668) ~[aviator-5.3.3.jar:na]
	at com.googlecode.aviator.AviatorEvaluatorInstance.execute(AviatorEvaluatorInstance.java:1685) ~[aviator-5.3.3.jar:na]
	at com.googlecode.aviator.AviatorEvaluator.execute(AviatorEvaluator.java:544) ~[aviator-5.3.3.jar:na]
	at movee.aviator.AviatorApplication.lambda$test$0(AviatorApplication.java:79) ~[classes/:na]
	at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:771) ~[spring-boot-3.1.2.jar:3.1.2]
	... 5 common frames omitted

将表达式改为[]访问属性,才会正常:

Object value = AviatorEvaluator.execute("a.b[0].['x']", jsonObj.toMap());

请问上述现象符合设计初衷吗?是否可以优化一下,list元素的属性也可以通过 . 访问?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant