fix: parseJson数组解析 + WebManager ClassCastException修复
- addParsedEntry增加JSON数组识别,存为List<Map>而非String - 新增parseJsonArray/parseJsonArrayItem递归解析嵌套数组 - 修复pollAdminChanges中List类型强转失败(class String cannot be cast to List)
This commit is contained in:
@@ -1868,6 +1868,13 @@ public class WebManager {
|
||||
} catch (NumberFormatException ignored) {
|
||||
}
|
||||
|
||||
// JSON数组 → List<Map>
|
||||
if (value.startsWith("[") && value.endsWith("]")) {
|
||||
List<Object> list = parseJsonArray(value);
|
||||
map.put(key, list);
|
||||
return;
|
||||
}
|
||||
|
||||
// 字符串(去掉引号)
|
||||
String cleaned = value.replaceAll("^\"|\"$", "")
|
||||
.replace("\\\"", "\"")
|
||||
@@ -1876,6 +1883,80 @@ public class WebManager {
|
||||
map.put(key, cleaned);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析JSON数组为List
|
||||
* 支持 [{...}, {...}] 和 ["a", "b"] 格式
|
||||
*/
|
||||
private static List<Object> parseJsonArray(String arrStr) {
|
||||
List<Object> list = new ArrayList<>();
|
||||
arrStr = arrStr.trim();
|
||||
if (arrStr.length() < 2) return list;
|
||||
arrStr = arrStr.substring(1, arrStr.length() - 1); // 去掉 [ ]
|
||||
|
||||
int depth = 0;
|
||||
boolean inString = false;
|
||||
StringBuilder current = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < arrStr.length(); i++) {
|
||||
char c = arrStr.charAt(i);
|
||||
if (inString) {
|
||||
if (c == '\\' && i + 1 < arrStr.length()) {
|
||||
current.append(c).append(arrStr.charAt(++i));
|
||||
} else if (c == '"') {
|
||||
inString = false;
|
||||
current.append(c);
|
||||
} else {
|
||||
current.append(c);
|
||||
}
|
||||
} else {
|
||||
if (c == '"') {
|
||||
inString = true;
|
||||
current.append(c);
|
||||
} else if (c == '{' || c == '[') {
|
||||
depth++;
|
||||
current.append(c);
|
||||
} else if (c == '}' || c == ']') {
|
||||
depth--;
|
||||
current.append(c);
|
||||
} else if (c == ',' && depth == 0) {
|
||||
// 分隔符
|
||||
String item = current.toString().trim();
|
||||
if (!item.isEmpty()) {
|
||||
list.add(parseJsonArrayItem(item));
|
||||
}
|
||||
current = new StringBuilder();
|
||||
} else {
|
||||
current.append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
String last = current.toString().trim();
|
||||
if (!last.isEmpty()) {
|
||||
list.add(parseJsonArrayItem(last));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private static Object parseJsonArrayItem(String item) {
|
||||
item = item.trim();
|
||||
if (item.startsWith("{") && item.endsWith("}")) {
|
||||
// 嵌套对象 → Map
|
||||
return parseJson(item);
|
||||
}
|
||||
if (item.startsWith("\"") && item.endsWith("\"")) {
|
||||
return item.substring(1, item.length() - 1)
|
||||
.replace("\\\"", "\"")
|
||||
.replace("\\n", "\n")
|
||||
.replace("\\\\", "\\");
|
||||
}
|
||||
if (item.equals("null")) return null;
|
||||
if (item.equals("true")) return true;
|
||||
if (item.equals("false")) return false;
|
||||
try { return Integer.parseInt(item); } catch (NumberFormatException ignored) {}
|
||||
try { return Double.parseDouble(item); } catch (NumberFormatException ignored) {}
|
||||
return item;
|
||||
}
|
||||
|
||||
// ==================== 业务功能 ====================
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user