Java JsonPath函数“ indexOf”给出异常:函数的参数:“ indexOf”未正确关闭

时间:2019-06-26 07:37:00

标签: java jsonpath

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

它给出了异常Arguments to function: 'indexOf' are not closed properly.,但在在线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" 但它给出了例外。这是堆栈跟踪

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.

1 个答案:

答案 0 :(得分:2)

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

因此,您可以切换库,也可以将表达式更改为Jayway版本可以使用的格式。如果您更改此路径,则您的代码应会为您提供预期的结果:

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