我有一个名为"results"
的ArrayNode:
[{
"subjectName": "Eclipse",
"code": "EC1",
"states": [
{
"subjectName": "LunorEx1",
"code":"E1"
},
{
"subjectName": "LunorEx2",
"code":"E2"
},
{
"subjectName": "Expus LunorEx3 ",
"code":"E6"
}]
},
{
"subjectName": "Lasena",
"code": "LS1",
"states": [
{
"subjectName": "SunorEx1",
"code":"S1"
},
{
"subjectName": "SunorEx2",
"code":"S2"
}]
}, {
"subjectName": "Gilesh",
"code": "GL2",
"states": [ ]
}]
此变量是公共变量。
使用Java 8,我希望能够检查是否存在,例如subjectName等于“ Eclipse”,代码等于“ EC1”,如果存在,那么我想在其json状态中进行搜索并找到一个状态,例如,其subjectName等于“ LunorEx1”,代码为“ E1”,如果全部找到,我想返回true
private static boolean subjectValidation( String parentSubjectName, String parentCode, String childSubjectName, String childCode){
boolean valid = false;
try {
JsonNode subjectData = StreamSupport.stream(results.spliterator(), true)
.filter(c -> c.get("subjectName").asText().equals(parentSubjectName) &&
c.get("code").asText().equals(parentCode) )
.collect(Collectors.toList()).get(0);
valid = StreamSupport.stream(subjectData.get("states").spliterator(), true)
.anyMatch(k -> k.get("states").get("subjectName").asText().equals(childSubjectName) &&
k.get("states").get("code").asText().equals(childCode));
} catch (Exception e) {
}
return valid;
}
我想优化此原因,因为我有几个具有相同结构的JSON数组,并且进行了一些类似的检查...。并在拳头一无所有时处理。 有人可以帮助我,给我一些建议,我如何做得更好?
答案 0 :(得分:0)
我认为它可能像这样:
private static boolean subjectValidation(String parentSubjectName, String parentCode, String childSubjectName,
String childCode) {
boolean valid = false;
try {
Optional<JsonNode> subjectData = StreamSupport.stream(results.spliterator(), true)
.filter(c -> exists(c, "subjectName", parentSubjectName) && exists(c, "code", parentCode))
.findFirst();
if (subjectData.isPresent()) {
valid = StreamSupport.stream(subjectData.get().get("states").spliterator(), true)
.anyMatch(k -> exists(k.get("states"), "subjectName", childSubjectName)
&& exists(k.get("states"), "code", childCode));
}
} catch (Exception e) {
}
return valid;
}
private static boolean exists(JsonNode node, String nodeName, String value) {
return node.get(nodeName).asText().equals(value);
}
答案 1 :(得分:0)
我想考虑一下subjectName和代码不是唯一的。
private static boolean subjectValidation(String parentSubjectName, String parentCode, String childSubjectName, String childCode) {
try {
return StreamSupport.stream(results.spliterator(), true)
.filter(c -> hasSubject(c, parentSubjectName) && hasCode(c, parentCode))
.flatmap(s -> StreamSupport.stream(s.get("states").spliterator(), true)
.map(k -> k.get("states"))
.anyMatch(k -> hasSubject(k, childSubjectName) && hasCode(k, childCode));
} catch (Exception e) {
return false;
}
}
private static boolean hasSubject(JsonNode node, String value) {
return fieldHasValue(node, "subjectName", value);
}
private static boolean hasCode(JsonNode node, String value) {
return fieldHasValue(node, "code", value);
}
private static boolean fieldHasValue(JsonNode node, String field, String value) {
return node.get(field).asText().equals(value);
}
也许应该有更好的异常处理,但是它应该可以工作。
如果您想进一步泛化,我将创建一个函数,让您可以简单地基于设置“代码”,“主题名称”和“状态”的变量来进行这些检查