读取xml

将json文件读取成字符串

css 复制代码
public static String getJsonStr(File jsonFile){
   try {
        FileReader fileReader = new FileReader(jsonFile);
        Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
        int ch = 0;
        StringBuffer sb = new StringBuffer();
        while ((ch = reader.read()) != -1) {
            sb.append((char) ch);
        }
        fileReader.close();
        reader.close();
        return sb.toString();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

main方法解析json文件,转化成自定义对象集合并保存至数据库

css 复制代码
public void Task1() {
    String json = "D:\\media\\resource\\RadarPic\\城镇预报数据.json";
    File jsonFile = new File(json);
    //通过上面那个方法获取json文件的内容
    String jsonData = this.getJsonStr(jsonFile);
    SevpChnWefcRffc sevpChnWefcRffc = JsonConverter.jsonString2Bean(jsonData, SevpChnWefcRffc.class);
    List<SevpChnWefcRffcItem> ds = sevpChnWefcRffc.getDs();
    for (List<SevpChnWefcRffcItem> l1 : ListUtils.splitList(ds, 20)) {
        if (l1.size() > 0) {
            int row1 = sevpChnWefcRffcMapper.insertSevpChnWefcRffc(l1);
        }
    }
}

json工具类

css 复制代码
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class JsonConverter {
    private static ObjectMapper mapper = new ObjectMapper();
    public static String bean2Json(Object obj) {
        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static <T> T jsonString2Bean(String jsonStr, Class<T> objClass) {
        if (jsonStr == null) return null;
        try {
            return mapper.readValue(jsonStr, objClass);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    public static <T> T jsonObject2Bean(Object jsonObject, Class<T> bean) {
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        return mapper.convertValue(jsonObject, bean);
    }

    public static JsonNode jsonString2JsonNode(String jsonString) {
        if (jsonString == null) return null;
        try {
            return mapper.readTree(jsonString);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Map<String, Object> jsonNode2HashMap(JsonNode jsonNode) {
        Map<String, Object> map = new HashMap<>();

        if (jsonNode.isObject()) {
            Iterator<Map.Entry<String, JsonNode>> fieldsIterator = jsonNode.fields();

            while (fieldsIterator.hasNext()) {
                Map.Entry<String, JsonNode> field = fieldsIterator.next();
                String key = field.getKey();
                JsonNode value = field.getValue();

                if (value.isValueNode()) {
                    map.put(key, value.asText());
                } else if (value.isObject() || value.isArray()) {
                    map.put(key, jsonNode2HashMap(value));
                }
            }
        } else if (jsonNode.isArray()) {
            int i = 0;

            for (JsonNode node : jsonNode) {
                if (node.isValueNode()) {
                    map.put(Integer.toString(i), node.asText());
                } else if (node.isObject() || node.isArray()) {
                    map.put(Integer.toString(i), jsonNode2HashMap(node));
                }
                i++;
            }
        }

        return map;
    }

    /**
     * 将JSON数据格式化并保存到文件中
     * @param jsonData 需要输出的json数
     * @param filePath 输出的文件地址
     * @return 结果
     */
    public static boolean saveJsonFile(Object jsonData, String filePath) {
        String content = JSON.toJSONString(jsonData, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat);
        boolean flag = true;
        try {
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                boolean mkdirs = file.getParentFile().mkdirs();
            }
            if (file.exists()) {
                boolean delete = file.delete();
            }
            boolean newFile = file.createNewFile();
            Writer write = new OutputStreamWriter(Files.newOutputStream(file.toPath()), StandardCharsets.UTF_8);
            write.write(content);
            write.flush();
            write.close();
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
        }
        return flag;
    }
}

自定义类

css 复制代码
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.List;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@EqualsAndHashCode
@Data
public class SevpChnWefcRffc {
    @JsonProperty(value = "DS")
    private List<SevpChnWefcRffcItem> ds;
}
css 复制代码
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Date;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@EqualsAndHashCode
@Data
public class SevpChnWefcRffcItem {
    /**
     * ID
     */
    private Integer id;

    /**
     * 省名
     */
    @JsonProperty(value = "Province")
    private String province;

    /**
     * 地市名
     */
    @JsonProperty(value = "City")
    private String city;

    /**
     * 行政编码
     */
    @JsonProperty(value = "Admin_Code_CHN")
    private String adminCodeChn;

    /**
     * 区县名
     */
    @JsonProperty(value = "Cnty")
    private String cnty;

    /**
     * 区站号
     */
    @JsonProperty(value = "Station_Id_C")
    private String stationIdC;

    /**
     * 站名
     */
    @JsonProperty(value = "Station_Name")
    private String stationName;

    /**
     * 测站级别
     */
    @JsonProperty(value = "Station_levl")
    private String stationLevel;

    /**
     * 纬度
     */
    @JsonProperty(value = "Lat")
    private String lat;

    /**
     * 经度
     */
    @JsonProperty(value = "Lon")
    private String lon;

    /**
     * 资料时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonProperty(value = "Datetime")
    private Date dateTime;
    /**
     * 产品描述
     */
    @JsonProperty(value = "PROD_DESC")
    private String prodDesc;

    /**
     * 产品代码
     */
    @JsonProperty(value = "PROD_CODE")
    private String prodCode;
    /**
     * 预报时效
     */
    @JsonProperty(value = "Validtime")
    private Integer validTime;

    /**
     * 温度
     */
    @JsonProperty(value = "TEM")
    private Double tem;

    /**
     * 气压
     */
    @JsonProperty(value = "PRS")
    private Double prs;

    /**
     * 风向
     */
    @JsonProperty(value = "WIN_D")
    private Double winD;

    /**
     * 风速
     */
    @JsonProperty(value = "WIN_S")
    private Double winS;

    /**
     * 相对湿度
     */
    @JsonProperty(value = "RHU")
    private Double rhu;

    /**
     * 可降水分
     */
    @JsonProperty(value = "PRE_PRE_Fore")
    private Double prePreFore;

    /**
     * 天气现象
     */
    @JsonProperty(value = "WEP")
    private Integer wep;

    /**
     * 能见度
     */
    @JsonProperty(value = "VIS")
    private Double vis;
}

大集合转化成若干个小集合

css 复制代码
import java.util.ArrayList;
import java.util.List;

public class ListUtils {
    /**
     * 将集合拆分成若干个batch,并将batch存于新的集合
     * @param list 目标集合
     * @param size batch大小
     * List m: [a, b, c, d, e, f, g]
     * -> splitList(m, 3)
     * -> [[a, b, c], [d, e, f], [g]]
     * @return List<Batch>
     */
    public static <T> List<List<T>> splitList(List<T> list, int size) {
        if (list == null) return null;
        List<List<T>> l1 = new ArrayList<>();
        if (list.size() <= size) l1.add(list);
        else {
            int s = list.size();
            int x = s / size;
            int y = s % size;
            if (y != 0) x = x + 1;
            int index = 0;
            for (int i = 0; i < x; i++) {
                List<T> l2 = new ArrayList<>();
                for (int j = 0; j < list.size(); j++) {
                    l2.add(list.get(index));
                    index++;
                    if (l2.size() == size) {
                        l1.add(l2);
                        break;
                    } else if (x == l1.size()+1 && y == l2.size()) {
                        l1.add(l2);
                        break;
                    }
                }
            }
        }
        return l1;
    }
}
相关推荐
chuanauc10 分钟前
Kubernets K8s 学习
java·学习·kubernetes
一头生产的驴26 分钟前
java整合itext pdf实现自定义PDF文件格式导出
java·spring boot·pdf·itextpdf
YuTaoShao33 分钟前
【LeetCode 热题 100】73. 矩阵置零——(解法二)空间复杂度 O(1)
java·算法·leetcode·矩阵
zzywxc78736 分钟前
AI 正在深度重构软件开发的底层逻辑和全生命周期,从技术演进、流程重构和未来趋势三个维度进行系统性分析
java·大数据·开发语言·人工智能·spring
YuTaoShao3 小时前
【LeetCode 热题 100】56. 合并区间——排序+遍历
java·算法·leetcode·职场和发展
程序员张33 小时前
SpringBoot计时一次请求耗时
java·spring boot·后端
llwszx6 小时前
深入理解Java锁原理(一):偏向锁的设计原理与性能优化
java·spring··偏向锁
云泽野6 小时前
【Java|集合类】list遍历的6种方式
java·python·list
二进制person7 小时前
Java SE--方法的使用
java·开发语言·算法
小阳拱白菜8 小时前
java异常学习
java