0

我正在尝试在 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]"));
            }
        }
    }
4

1 回答 1

1

您的 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 + "]"));
        }
    }
}
于 2013-09-03T22:15:33.737 回答