代码拉取完成,页面将自动刷新
package com.hk.xieyi.dubbo;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String ruleString = "{\"wendu\":\" >= 4.001\",\"shidu\":\" <= 100.001\",\"rain\":\" = 0\",\"wind\":\" > 11\",\"ss\": \"1.00,2.00,3.00,4\",\"qq\": \"1,2,3.02,4\"}";
String parameterString = "{\"wendu\":\"4.001\",\"qq\": \" 3.02\",\"ss\": \" 2.00\"}";
System.out.println(System.getProperty("java.library.path"));
String filteredParams = filterParameters(ruleString, parameterString);
System.out.println(filteredParams);
}
public static String filterParameters(String ruleString, String parameterString) {
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode ruleJson = mapper.readTree(ruleString);
JsonNode parameterJson = mapper.readTree(parameterString);
Map<String, Object> filteredParams = filterParameters(ruleJson, parameterJson);
return mapper.writeValueAsString(filteredParams);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static Map<String, Object> filterParameters(JsonNode ruleJson, JsonNode parameterJson) {
Map<String, Object> filteredParams = new HashMap<>();
ruleJson.fields().forEachRemaining(entry -> {
String key = entry.getKey();
JsonNode ruleNode = entry.getValue();
String rule = ruleNode.isTextual() ? ruleNode.asText() : ruleNode.toString();
if (parameterJson.has(key)) {
JsonNode valueNode = parameterJson.get(key);
if (evaluateRule(rule, valueNode)) {
// 只有满足规则的参数才会被添加到输出中
filteredParams.put(key, valueNode.asText());
}
} else {
System.err.println("Parameter key not found: " + key);
}
});
return filteredParams;
}
// 判断规则是否满足参数值
private static boolean evaluateRule(String rule, JsonNode valueNode) {
if (rule.contains(",")) {
String[] split = rule.split(",");
double value = valueNode.asDouble();
for (String ss : split) {
double v = Double.parseDouble(ss);
if (v == value) {
return true;
}
}
} else {
// 使用正则表达式匹配运算符和阈值
Pattern pattern = Pattern.compile("([><=!]+)\\s*([\\d.]+)");
Matcher matcher = pattern.matcher(rule);
if (matcher.find()) {
String operator = matcher.group(1);
try {
double threshold = Double.parseDouble(matcher.group(2));
double value = valueNode.asDouble();
switch (operator) {
case ">=":
return value >= threshold;
case "=":
return value == threshold;
case "<=":
return value <= threshold;
case "!=":
return value != threshold;
case ">":
return value > threshold;
case "<":
return value < threshold;
default:
return false;
}
} catch (NumberFormatException e) {
System.err.println("Invalid threshold format: " + matcher.group(2));
return false;
}
} else {
System.err.println("Invalid rule format: " + rule);
return false;
}
}
return false;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。