JAVA动态建立ANDED IF语句

时间:2018-11-29 08:25:43

标签: java json

如果我有5个由GET API调用传递的可选参数,我该如何过滤JSON数组并获得所需的结果。

基本上,如果全部5个参数都传递了,我可以按如下所示签入“ ANDED” IF语句

if (obj.get(address).toString().contains(searchRequest.getAddress())
                        && obj.get(car).toString().equalsIgnoreCase(searchRequest.getCar())
                        && properties.get(name).toString().equalsIgnoreCase(searchRequest.getName())
                        && properties.get(job).toString().equalsIgnoreCase(searchRequest.getJob())
                        && properties.get(salary).toString().equalsIgnoreCase(searchRequest.getSalary())

searchRequest包含5个字段,这些字段可以可选地传递给GET REST调用,它将很好地工作,但是如果仅传递两个字段,这允许我如何建立正确的ANDED IF语句,例如,如果仅searchRequest.getAddress()和searchRequest.getName()已传递,我想要

 if (obj.get(address).toString().contains(searchRequest.getAddress())
                        && obj.get(name).toString().equalsIgnoreCase(searchRequest.getName())

并忽略其他3个字段

1 个答案:

答案 0 :(得分:0)

使用比较方法:

public static <T> boolean equalsIgnoreCase(T a, T b) {
    if (a == b) {
        return true;
    } else if (a == null || b == null) {
        return false;
    } else {
        return a.toString().equalsIgnoreCase(b.toString());
    }
}