java - Java JsonPath 表达式中的计数器

标签 java variables expression counter jsonpath

我尝试在 JSON 路径表达式中使用计数器并收到 JSONPath 错误 java.lang.numberFormatException

    for (int counter = 0; counter < ids.size(); counter++) {
        tmp_rules = JsonPath.read(jsonFile, "$..orders[counter].rule");
        for (int counter2 = 0; counter2 < tmp_rules.size();counter2++){
            if (
                    (JsonPath.read(jsonFile, "$..orders[counter].rule[counter2]") == 1) &&
                    (JsonPath.read(jsonFile, "$..orders[counter].asked[counter2]")) != 0) {
                       end_id.add(JsonPath.read(jsonFile, "$..id[counter]"));
                       end_rule.add(JsonPath.read(jsonFile, "$..orders[counter].rule[counter2]"));
                       end_asked.add(JsonPath.read(jsonFile,"$..orders[counter].asked[counter2]"));
            }
        }
    }

最佳答案

您的 JSON 路径表达式无效,因为您使用 countercounter2 字符串作为索引数组。您应该在路径表达式中使用循环变量的:

for (int counter = 0; counter < ids.size(); counter++) {
    tmp_rules = JsonPath.read(jsonFile, "$..orders[" + counter + "].rule");
    for (int counter2 = 0; counter2 < tmp_rules.size();counter2++){
        if (
                (JsonPath.read(jsonFile, "$..orders[" + counter + "].rule[" + counter2 + "]") == 1) &&
                (JsonPath.read(jsonFile, "$..orders[" + counter + "].asked[" + counter2 + "]")) != 0) {
                   end_id.add(JsonPath.read(jsonFile, "$..id[" + counter + "]"));
                   end_rule.add(JsonPath.read(jsonFile, "$..orders[" + counter + "].rule[" + counter2 + "]"));
                   end_asked.add(JsonPath.read(jsonFile,"$..orders[" + counter + "].asked[" + counter2 + "]"));
        }
    }
}

关于java - Java JsonPath 表达式中的计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18602179/

相关文章:

java - Spymemcached 集在多线程和高负载下使用时不起作用

java - Powermock junit5 和mockito2.x 不工作 RunnerTestSuiteChunker 未找到

java - 如何跳过初始数据并仅触发 Firestore Firebase 中的新更新?

java - Eclipse 调试器中变量条目旁边的 id=xxx 是什么

php - 如何在 MySQL 语句中包含 PHP 变量

c# - Expression.Equal - 如何比较可空字段和不可空字段?

php - 使用 php 变量来操作 html 的标题和正文的 Id (有人这样做吗?)

php - 如何在MySQL数据库中插入可变数据

c# - 运行时在表达式树中链接方法调用

c# - Entity Framework 结合来自不同实体的 lambda 表达式