fix: JSON无页面规则时兜底展示原始结构,清理重复代码
This commit is contained in:
@@ -41,26 +41,13 @@ public class AdapterEngine {
|
|||||||
result.put("contentType", "json");
|
result.put("contentType", "json");
|
||||||
|
|
||||||
Map.Entry<String, PageConfig> matched = matchPage(config.getPages(), accessUrl);
|
Map.Entry<String, PageConfig> matched = matchPage(config.getPages(), accessUrl);
|
||||||
if (matched == null) {
|
|
||||||
result.put("matched", false);
|
|
||||||
result.put("message", "未匹配到页面规则");
|
|
||||||
result.put("availablePages", new ArrayList<>(config.getPages() != null ? config.getPages().keySet() : Set.of()));
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (matched != null && matched.getValue().getFields() != null && !matched.getValue().getFields().isEmpty()) {
|
||||||
|
// 有页面规则 → 按 selector 提取
|
||||||
PageConfig page = matched.getValue();
|
PageConfig page = matched.getValue();
|
||||||
result.put("matched", true);
|
result.put("matched", true);
|
||||||
result.put("pageName", matched.getKey());
|
result.put("pageName", matched.getKey());
|
||||||
|
|
||||||
if (page.getFields() == null || page.getFields().isEmpty()) {
|
|
||||||
result.put("message", "页面规则未定义字段");
|
|
||||||
result.put("extracted", List.of());
|
|
||||||
result.put("rowCount", 0);
|
|
||||||
result.put("fieldCount", 0);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 列表容器
|
|
||||||
List<Object> items = new ArrayList<>();
|
List<Object> items = new ArrayList<>();
|
||||||
if (page.getListContainer() != null && !page.getListContainer().isBlank()) {
|
if (page.getListContainer() != null && !page.getListContainer().isBlank()) {
|
||||||
Object container = resolveJsonPath(parsed, page.getListContainer());
|
Object container = resolveJsonPath(parsed, page.getListContainer());
|
||||||
@@ -70,7 +57,6 @@ public class AdapterEngine {
|
|||||||
items.add(parsed);
|
items.add(parsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 逐行提取字段
|
|
||||||
List<Map<String, Object>> rows = new ArrayList<>();
|
List<Map<String, Object>> rows = new ArrayList<>();
|
||||||
int max = Math.min(items.size(), 50);
|
int max = Math.min(items.size(), 50);
|
||||||
for (int i = 0; i < max; i++) {
|
for (int i = 0; i < max; i++) {
|
||||||
@@ -97,6 +83,35 @@ public class AdapterEngine {
|
|||||||
result.put("rowCount", rows.size());
|
result.put("rowCount", rows.size());
|
||||||
result.put("fieldCount", page.getFields().size());
|
result.put("fieldCount", page.getFields().size());
|
||||||
result.put("message", rows.isEmpty() ? "selector 未匹配到数据" : "共 " + rows.size() + " 行");
|
result.put("message", rows.isEmpty() ? "selector 未匹配到数据" : "共 " + rows.size() + " 行");
|
||||||
|
} else {
|
||||||
|
// 无页面规则 → 兜底展示原始结构
|
||||||
|
String reason = matched == null ? "未匹配到页面规则" : "页面规则未定义字段";
|
||||||
|
result.put("matched", true);
|
||||||
|
result.put("pageName", "JSON原始结构");
|
||||||
|
result.put("message", reason + ",展示原始数据");
|
||||||
|
List<Map<String, Object>> rows = new ArrayList<>();
|
||||||
|
if (parsed instanceof Map) {
|
||||||
|
Map<String, Object> row = new LinkedHashMap<>();
|
||||||
|
for (String key : ((Map<String, Object>) parsed).keySet()) {
|
||||||
|
row.put(key, toDisplayString(((Map<String, Object>) parsed).get(key)));
|
||||||
|
}
|
||||||
|
rows.add(row);
|
||||||
|
} else if (parsed instanceof List) {
|
||||||
|
int m = Math.min(((List<?>) parsed).size(), 10);
|
||||||
|
for (int i = 0; i < m; i++) {
|
||||||
|
Map<String, Object> row = new LinkedHashMap<>();
|
||||||
|
row.put("value", ((List<?>) parsed).get(i));
|
||||||
|
rows.add(row);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Map<String, Object> row = new LinkedHashMap<>();
|
||||||
|
row.put("value", parsed);
|
||||||
|
rows.add(row);
|
||||||
|
}
|
||||||
|
result.put("extracted", rows);
|
||||||
|
result.put("rowCount", rows.size());
|
||||||
|
result.put("fieldCount", rows.isEmpty() ? 0 : rows.get(0).size());
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
result.put("contentType", "json");
|
result.put("contentType", "json");
|
||||||
result.put("matched", false);
|
result.put("matched", false);
|
||||||
|
|||||||
Reference in New Issue
Block a user