读取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;
    }
}
相关推荐
Jason-河山6 分钟前
利用 Python 爬虫采集 1688商品详情
java·http
计算机源码社7 分钟前
分享一个餐饮连锁店点餐系统 餐馆食材采购系统Java、python、php三个版本(源码、调试、LW、开题、PPT)
java·python·php·毕业设计项目·计算机课程设计·计算机毕业设计源码·计算机毕业设计选题
Zww089110 分钟前
idea插件市场安装没反应
java·ide·intellij-idea
夜雨翦春韭12 分钟前
【代码随想录Day31】贪心算法Part05
java·数据结构·算法·leetcode·贪心算法
计算机学姐12 分钟前
基于微信小程序的调查问卷管理系统
java·vue.js·spring boot·mysql·微信小程序·小程序·mybatis
problc23 分钟前
Android 组件化利器:WMRouter 与 DRouter 的选择与实践
android·java
程序员南飞2 小时前
ps aux | grep smart_webrtc这条指令代表什么意思
java·linux·ubuntu·webrtc
弥琉撒到我2 小时前
微服务swagger解析部署使用全流程
java·微服务·架构·swagger
一颗花生米。3 小时前
深入理解JavaScript 的原型继承
java·开发语言·javascript·原型模式
问道飞鱼3 小时前
Java基础-单例模式的实现
java·开发语言·单例模式