Browse Source

xml list to json 方法

andy 1 year ago
parent
commit
4ec96ac2fd
1 changed files with 26 additions and 4 deletions
  1. 26 4
      src/main/java/com/scbfkj/uni/library/DataFormatUtil.java

+ 26 - 4
src/main/java/com/scbfkj/uni/library/DataFormatUtil.java

@@ -14,11 +14,13 @@ import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
 import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
 import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
 
+import java.text.SimpleDateFormat;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.concurrent.atomic.AtomicReference;
+import java.util.stream.Collectors;
 
 public final class DataFormatUtil {
     private static final AtomicReference<ObjectMapper> objectMapper = new AtomicReference<>();
@@ -73,7 +75,11 @@ public final class DataFormatUtil {
             return LocalDate.parse(str);
         } else if (dateObj instanceof LocalDate date) {
             return date;
-        } else {
+        } else if (dateObj instanceof Date date) {
+            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:dd:ss");
+            String formatDate = simpleDateFormat.format(date);
+            return LocalDate.parse(formatDate);
+        }  else {
             return null;
         }
     }
@@ -83,6 +89,10 @@ public final class DataFormatUtil {
             return LocalDateTime.parse(str);
         } else if (dateTimeObj instanceof LocalDateTime dateTime) {
             return dateTime;
+        } else if (dateTimeObj instanceof Date date) {
+            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:dd:ss");
+            String formatDate = simpleDateFormat.format(date);
+            return LocalDateTime.parse(formatDate);
         } else {
             return null;
         }
@@ -199,11 +209,23 @@ public final class DataFormatUtil {
         return xmlMapper.get();
     }
 
-    public static String xmltojson(String xml) throws JsonProcessingException {
+    public static List<String> xmltojson(List<String> xmls) {
+        return xmls.stream().map(xml -> {
+            Object json = null;
+            try {
+                json = getXmlMapper().readValue(xml, Object.class);
+                return getObjectMapper().writeValueAsString(json);
+            } catch (JsonProcessingException e) {
+                throw new RuntimeException(e);
+            }
+
+        }).collect(Collectors.toList());
+    }
+
+    public static String xml2json(String xml) throws JsonProcessingException {
         Object json = getXmlMapper().readValue(xml, Object.class);
 
-        String jsonStr = getObjectMapper().writeValueAsString(json);
-        return jsonStr;
+        return getObjectMapper().writeValueAsString(json);
     }
 
     public static List<Map<String, List<List<String>>>> typeBtoMap(List<String> datas) {