我尝试在 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 路径表达式无效,因为您使用 counter
和 counter2
字符串作为索引数组。您应该在路径表达式中使用循环变量的值:
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/