pom依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version> <!-- 使用最新版本 -->
</dependency>
具体方法
jsonConfig 例:
String jsonConfig = "[{\"expression\":\"!=\",\"fields\":\"status\",\"value\":\"CAMPAIGN_STATUS_ENABLE\"}," + "{\"expression\":\">\",\"fields\":\"budget\",\"value\":\"99999\"}]";
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class YourClass {
// ...
public boolean checkConditions(String jsonConfig, Object ocAdGroup) {
try {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(jsonConfig);
for (JsonNode conditionNode : rootNode) {
String expression = conditionNode.get("expression").asText();
String fields = conditionNode.get("fields").asText();
String value = conditionNode.get("value").asText();
// Check if the 'fields' property exists in the OcAdGroup class
if (!hasProperty(ocAdGroup, fields)) {
throw new IllegalArgumentException("Field '" + fields + "' does not exist in OcAdGroup.");
}
// Use reflection to get the value of the 'fields' property in OcAdGroup
Object fieldValue = getProperty(ocAdGroup, fields);
// Perform the comparison based on the 'expression' and 'value' in the condition
boolean conditionSatisfied = checkCondition(expression, fieldValue, value);
if (!conditionSatisfied) {
return false; // Condition not satisfied
}
}
return true; // All conditions satisfied
} catch (Exception e) {
// Handle exceptions (e.g., JSON parsing, reflection errors)
e.printStackTrace();
return false; // An error occurred
}
}
private boolean hasProperty(Object object, String propertyName) {
try {
object.getClass().getDeclaredField(propertyName);
return true;
} catch (NoSuchFieldException e) {
return false;
}
}
private Object getProperty(Object object, String propertyName) throws Exception {
java.lang.reflect.Field field = object.getClass().getDeclaredField(propertyName);
field.setAccessible(true);
return field.get(object);
}
private boolean checkCondition(String expression, Object fieldValue, String value) {
// Implement your condition checking logic here
// For example, if expression is '!=':
// return !fieldValue.equals(value);
// For other expressions, implement the corresponding checks.
return false;
}
// ...
}
补充 checkCondition()方法
private static boolean checkCondition(String expression, Object fieldValue, String value) {
try {
if (fieldValue instanceof String) {
String strValue = (String) fieldValue;
switch (expression) {
case "==":
return strValue.equals(value);
case "!=":
return !strValue.equals(value);
default:
return false;
}
} else if (fieldValue instanceof Integer || fieldValue instanceof Long) {
long longValue = ((Number) fieldValue).longValue();
long longConditionValue = Long.parseLong(value);
switch (expression) {
case "==":
return longValue == longConditionValue;
case "!=":
return longValue != longConditionValue;
case ">":
return longValue > longConditionValue;
case "<":
return longValue < longConditionValue;
case ">=":
return longValue >= longConditionValue;
case "<=":
return longValue <= longConditionValue;
default:
return false;
}
} else if (fieldValue instanceof Double || fieldValue instanceof Float) {
double doubleValue = ((Number) fieldValue).doubleValue();
double doubleConditionValue = Double.parseDouble(value);
switch (expression) {
case "==":
return doubleValue == doubleConditionValue;
case "!=":
return doubleValue != doubleConditionValue;
case ">":
return doubleValue > doubleConditionValue;
case "<":
return doubleValue < doubleConditionValue;
case ">=":
return doubleValue >= doubleConditionValue;
case "<=":
return doubleValue <= doubleConditionValue;
default:
return false;
}
} else {
return false;
}
} catch (NumberFormatException e) {
return false;
}
}