gpt4 book ai didi

Java JsonPath函数 "indexOf"给出异常: Arguments to function: 'indexOf' are not closed properly

转载 作者:行者123 更新时间:2023-12-02 01:26:46 28 4
gpt4 key购买 nike

我正在使用 Jayway JsonPath 库版本 2.4.0。在 jsonPath 中使用 indexOf 函数,例如。 $.values[?(@.num.indexOf('101') != -1)].id

它给出异常函数参数:'indexOf'未正确关闭。但在线 jsonPath 评估器上的效果相同

我在 https://jsonpath.com 上尝试了相同的 jsonPath 和 json 字符串。那里工作得很好。我尝试过一些混合和匹配(例如 $.values[?(@.num.indexOf("101") != -1)].id) (双引号而不是单引号)但还是一样。

public static void main(String[] args) {
try {
String jsonString ="{ \n" +
" \"values\":[ \n" +
" { \n" +
" \"num\":[\"101\", \"102\"],\n" +
" \"id\": \"a\"\n" +
" },\n" +
" { \n" +
" \"num\":[\"104\", \"105\"],\n" +
" \"id\": \"b\"\n" +
" }\n" +
" ]\n" +
"}";



String jsonPath = "$.values[?(@.num.indexOf('101') != -1)].id";


DocumentContext jsonContext = JsonPath.parse(jsonString);
List<String> ans = null;
ans = jsonContext.read(String.valueOf(jsonPath));
System.out.println(ans);
}catch(Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}

预期答案是“a”但它给出了异常(exception)。这是堆栈跟踪

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
com.jayway.jsonpath.InvalidPathException: Arguments to function: 'indexOf' are not closed properly.
at com.jayway.jsonpath.internal.path.PathCompiler.parseFunctionParameters(PathCompiler.java:375)
at com.jayway.jsonpath.internal.path.PathCompiler.readPropertyOrFunctionToken(PathCompiler.java:217)
at com.jayway.jsonpath.internal.path.PathCompiler.readNextToken(PathCompiler.java:151)
at com.jayway.jsonpath.internal.path.PathCompiler.readDotToken(PathCompiler.java:171)
at com.jayway.jsonpath.internal.path.PathCompiler.readNextToken(PathCompiler.java:145)
at com.jayway.jsonpath.internal.path.PathCompiler.readPropertyOrFunctionToken(PathCompiler.java:237)
at com.jayway.jsonpath.internal.path.PathCompiler.readNextToken(PathCompiler.java:151)
at com.jayway.jsonpath.internal.path.PathCompiler.readDotToken(PathCompiler.java:171)
at com.jayway.jsonpath.internal.path.PathCompiler.readNextToken(PathCompiler.java:145)
at com.jayway.jsonpath.internal.path.PathCompiler.readContextToken(PathCompiler.java:124)
at com.jayway.jsonpath.internal.path.PathCompiler.compile(PathCompiler.java:58)
at com.jayway.jsonpath.internal.path.PathCompiler.compile(PathCompiler.java:75)
at com.jayway.jsonpath.internal.filter.ValueNodes$PathNode.<init>(ValueNodes.java:580)
at com.jayway.jsonpath.internal.filter.ValueNode.createPathNode(ValueNode.java:208)
at com.jayway.jsonpath.internal.filter.FilterCompiler.readPath(FilterCompiler.java:364)
at com.jayway.jsonpath.internal.filter.FilterCompiler.readValueNode(FilterCompiler.java:96)
at com.jayway.jsonpath.internal.filter.FilterCompiler.readExpression(FilterCompiler.java:190)
at com.jayway.jsonpath.internal.filter.FilterCompiler.readLogicalANDOperand(FilterCompiler.java:186)
at com.jayway.jsonpath.internal.filter.FilterCompiler.readLogicalAND(FilterCompiler.java:150)
at com.jayway.jsonpath.internal.filter.FilterCompiler.readLogicalOR(FilterCompiler.java:132)
at com.jayway.jsonpath.internal.filter.FilterCompiler.readLogicalANDOperand(FilterCompiler.java:181)
at com.jayway.jsonpath.internal.filter.FilterCompiler.readLogicalAND(FilterCompiler.java:150)
at com.jayway.jsonpath.internal.filter.FilterCompiler.readLogicalOR(FilterCompiler.java:132)
at com.jayway.jsonpath.internal.filter.FilterCompiler.compile(FilterCompiler.java:78)
at com.jayway.jsonpath.internal.filter.FilterCompiler.compile(FilterCompiler.java:54)
at com.jayway.jsonpath.internal.path.PathCompiler.readFilterToken(PathCompiler.java:461)
at com.jayway.jsonpath.internal.path.PathCompiler.readNextToken(PathCompiler.java:141)
at com.jayway.jsonpath.internal.path.PathCompiler.readPropertyOrFunctionToken(PathCompiler.java:237)
at com.jayway.jsonpath.internal.path.PathCompiler.readNextToken(PathCompiler.java:151)
at com.jayway.jsonpath.internal.path.PathCompiler.readDotToken(PathCompiler.java:171)
at com.jayway.jsonpath.internal.path.PathCompiler.readNextToken(PathCompiler.java:145)
at com.jayway.jsonpath.internal.path.PathCompiler.readContextToken(PathCompiler.java:124)
at com.jayway.jsonpath.internal.path.PathCompiler.compile(PathCompiler.java:58)
at com.jayway.jsonpath.internal.path.PathCompiler.compile(PathCompiler.java:75)
at com.jayway.jsonpath.JsonPath.<init>(JsonPath.java:97)
at com.jayway.jsonpath.JsonPath.compile(JsonPath.java:463)
at com.jayway.jsonpath.internal.JsonContext.pathFromCache(JsonContext.java:222)
at com.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:78)
at Init.main(Init.java:32)
Arguments to function: 'indexOf' are not closed properly.

最佳答案

JsonPath 有多种实现。您的路径表达式与 Jayway 实现不兼容。我认为这是该实现中的一个错误。

所以你可以切换库,或者你可以将你的表达式更改为 Jayway 版本可以接受的内容。如果您更改此路径,您的代码应该会给出您期望的结果:

String jsonPath = "$.values[?('101' in @.num)].id";

关于Java JsonPath函数 "indexOf"给出异常: Arguments to function: 'indexOf' are not closed properly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56767604/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com