|
@@ -94,9 +94,39 @@ public class ProductSkuService {
|
|
|
}
|
|
}
|
|
|
String trimmed = specs.trim();
|
|
String trimmed = specs.trim();
|
|
|
// 已是合法 JSON 对象/数组,直接返回
|
|
// 已是合法 JSON 对象/数组,直接返回
|
|
|
- if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
|
|
|
|
|
|
|
+ if (trimmed.startsWith("{")) {
|
|
|
return trimmed;
|
|
return trimmed;
|
|
|
}
|
|
}
|
|
|
|
|
+ // 数组格式转 JSON 对象:["红色","M码"] → {"规格":"红色","规格2":"M码"}
|
|
|
|
|
+ if (trimmed.startsWith("[")) {
|
|
|
|
|
+ if (trimmed.contains("[object Object]") || trimmed.contains("[undefined]")) {
|
|
|
|
|
+ return "{}";
|
|
|
|
|
+ }
|
|
|
|
|
+ String body = trimmed.substring(1, trimmed.length() - 1);
|
|
|
|
|
+ Map<String, String> map = new LinkedHashMap<>();
|
|
|
|
|
+ int idx = 1;
|
|
|
|
|
+ for (String item : body.split(",", -1)) {
|
|
|
|
|
+ item = item.trim();
|
|
|
|
|
+ if (item.isEmpty()) continue;
|
|
|
|
|
+ if (item.startsWith("\"")) item = item.substring(1);
|
|
|
|
|
+ if (item.endsWith("\"")) item = item.substring(0, item.length() - 1);
|
|
|
|
|
+ map.put("规格" + idx, item);
|
|
|
|
|
+ idx++;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (map.isEmpty()) {
|
|
|
|
|
+ return "{}";
|
|
|
|
|
+ }
|
|
|
|
|
+ StringBuilder sb = new StringBuilder("{");
|
|
|
|
|
+ boolean first = true;
|
|
|
|
|
+ for (Map.Entry<String, String> entry : map.entrySet()) {
|
|
|
|
|
+ if (!first) sb.append(",");
|
|
|
|
|
+ first = false;
|
|
|
|
|
+ sb.append("\"").append(escapeJson(entry.getKey())).append("\":");
|
|
|
|
|
+ sb.append("\"").append(escapeJson(entry.getValue())).append("\"");
|
|
|
|
|
+ }
|
|
|
|
|
+ sb.append("}");
|
|
|
|
|
+ return sb.toString();
|
|
|
|
|
+ }
|
|
|
// 将 k1:v1;k2:v2 格式转为 JSON 对象
|
|
// 将 k1:v1;k2:v2 格式转为 JSON 对象
|
|
|
Map<String, String> map = new LinkedHashMap<>();
|
|
Map<String, String> map = new LinkedHashMap<>();
|
|
|
String[] pairs = trimmed.split("[;;]");
|
|
String[] pairs = trimmed.split("[;;]");
|