读取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;
    }
}
相关推荐
阿伟*rui1 小时前
配置管理,雪崩问题分析,sentinel的使用
java·spring boot·sentinel
待磨的钝刨1 小时前
【格式化查看JSON文件】coco的json文件内容都在一行如何按照json格式查看
开发语言·javascript·json
XiaoLeisj3 小时前
【JavaEE初阶 — 多线程】单例模式 & 指令重排序问题
java·开发语言·java-ee
paopaokaka_luck3 小时前
【360】基于springboot的志愿服务管理系统
java·spring boot·后端·spring·毕业设计
dayouziei3 小时前
java的类加载机制的学习
java·学习
Yaml45 小时前
Spring Boot 与 Vue 共筑二手书籍交易卓越平台
java·spring boot·后端·mysql·spring·vue·二手书籍
小小小妮子~5 小时前
Spring Boot详解:从入门到精通
java·spring boot·后端
hong1616885 小时前
Spring Boot中实现多数据源连接和切换的方案
java·spring boot·后端
aloha_7896 小时前
从零记录搭建一个干净的mybatis环境
java·笔记·spring·spring cloud·maven·mybatis·springboot
记录成长java6 小时前
ServletContext,Cookie,HttpSession的使用
java·开发语言·servlet