Java行政区划代码处理,包含源JSON文件,处理过后的JSON、Excel、SQL文件

参考

民政部最新的行政区划代码

前言

一开始是想直接解析民政部的html,获取里面的数据,然后我发现这个html不规范,解析一直报错,什么xx标签的属性值应当要引号、xx标签没有闭合。。。
在这里插入图片描述
所以我放弃了解析html的想法,直接找前端要了份数据。

数据

我需要的格式:

DROP TABLE IF EXISTS `sys_city`;
CREATE TABLE `sys_city`  (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT 'id',
  `pid` bigint NULL DEFAULT NULL COMMENT '父级id',
  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '名称',
  `code` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '编码',
  `intact_code` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '完整编码',
  `level` int NULL DEFAULT NULL COMMENT '等级(1 省级 2 市级 3 县级)',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 249 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '全国各省市区县代码' ROW_FORMAT = DYNAMIC;

先看看这几份数据:
Java行政区划代码处理,包含源JSON文件,处理过后的JSON、Excel、SQL文件_第1张图片

1、data.json

Java行政区划代码处理,包含源JSON文件,处理过后的JSON、Excel、SQL文件_第2张图片

2、demo.json

Java行政区划代码处理,包含源JSON文件,处理过后的JSON、Excel、SQL文件_第3张图片

3、source.json

Java行政区划代码处理,包含源JSON文件,处理过后的JSON、Excel、SQL文件_第4张图片

4、target.json

Java行政区划代码处理,包含源JSON文件,处理过后的JSON、Excel、SQL文件_第5张图片

5、target.xls(直接将这份数据导进数据库中)

Java行政区划代码处理,包含源JSON文件,处理过后的JSON、Excel、SQL文件_第6张图片

6、targetSql.json

Java行政区划代码处理,包含源JSON文件,处理过后的JSON、Excel、SQL文件_第7张图片

数据处理

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.common.util.TreeUtil;
import lombok.Data;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Main {

    @Data
    public static class Entity{
        private String value;
        private String label;
        private List<Entity> children;
    }

    @Data
    public static class SysCity{
        private String id;
        private String pid;
        private String name;
        private String code;
        private String intactCode;
        private Integer level;
        public List<SysCity> children = new ArrayList<>();
    }

	/**
     * 输出到txt文件
     */
    private static void exportTxt(List<SysCity> dataList) throws IOException {
        String content = JSONUtil.toJsonStr(dataList);
        String path = "C:\\Users\\Admin\\Desktop\\target.txt";
        FileWriter fileWriter = new FileWriter(path,false);
        fileWriter.write(content);
        fileWriter.flush();
        fileWriter.close();
    }

    /**
     * 输出到excel文件
     */
    private static void exportExcel(List<SysCity> dataList) throws IOException {
        List<List<String>> rowAll = new ArrayList<List<String>>();
        List<String> row = CollUtil.newArrayList("id", "pid", "name", "code", "intact_code","level");
        rowAll.add(row);
        // 循环添加数据到excel中
        for (int i = 0; i < dataList.size(); i++) {
            SysCity city = dataList.get(i);
            List<String> rowItem = CollUtil.newArrayList(city.getId(),city.getPid(),city.getName(),city.getCode(),city.getIntactCode(),String.valueOf(city.getLevel()));
            rowAll.add(rowItem);
        }
        ExcelWriter writer = ExcelUtil.getWriter();
        writer.setColumnWidth(-1, 30);
        writer.setRowHeight(-1,20);
        FileOutputStream output = new FileOutputStream("C:\\Users\\Admin\\Desktop\\target.xls");
        // 一次性写出内容
        writer.write(rowAll);
        writer.flush(output);
        // 关闭writer,释放内存
        writer.close();
    }

    /**
     * 输出到json文件
     */
    private static void exportJson(List<SysCity> dataList) throws IOException {
    	// 输出的json需要有层级结构,所以需要将dataList构建为树结构
        List<SysCity> list = TreeUtil.buildTree(dataList, "0", "pid");
        String content = JSONUtil.toJsonStr(list);
        String path = "C:\\Users\\Admin\\Desktop\\target.json";
        FileWriter fileWriter = new FileWriter(path,false);
        fileWriter.write(content);
        fileWriter.flush();
        fileWriter.close();
    }

    public static void main(String[] args) throws IOException {
        List<Entity> list = readSourceTxt();
        List<SysCity> dataList = new ArrayList<>();
        int id = 1;
        String pid="0";
        getCityList(list,dataList,id,pid);
        // 生成txt文件
        exportTxt(dataList);
        // 生成excel
        exportExcel(dataList);
        // 生成json文件
        exportJson(dataList);
    }

    /**
     * 读取源文件,解析为json格式,添加到list中
     */
    private static List<Entity> readSourceTxt() throws IOException {
        String path = System.getProperty("user.dir")+"\\src\\test\\java\\com\\source.txt";
        File file = new File(path);
        BufferedReader br = new BufferedReader(new FileReader(file));
        StringBuilder sb = new StringBuilder();
        String st;
        while ((st = br.readLine()) != null) {
            sb.append(st);
        }
        return JSONUtil.toList(sb.toString(), Entity.class);
    }

    /**
     * 数据处理
     * @param list 原始数据
     * @param dataList 目标数据
     * @param id id
     * @param pid 父级id
     */
    public static int getCityList(List<Entity> list,List<SysCity> dataList,int id,String pid){
        for (Entity entity : list) {
            SysCity city = new SysCity();
            city.setId(String.valueOf(id));
            city.setName(entity.getLabel());
            city.setIntactCode(entity.getValue());
            String code = entity.getValue();
            //源数据的编码都是六位数,省级后面四位都是0,去掉;市级后面两位是0,去掉;县(区)级取全部
            if (code.endsWith("0000")){
                code = code.replace("0000","");
                city.setLevel(1);
            }else if (code.endsWith("00")){
                code = code.substring(0,4);
                city.setLevel(2);
            }else {
                city.setLevel(3);
            }
            //code只取最后两位,完整的编码前面几位都是由它的父级组成的,intactCode存储的是完整的编码
            city.setCode(code.substring(code.length()-2));
            city.setPid(pid);
            dataList.add(city);
            id++;
            List<Entity> children = entity.getChildren();
            if (children != null && !children.isEmpty()){
                id = getCityList(children,dataList,id, city.getId());
            }
        }
        return id;
    }
}
/**
 * 构建树结构工具类
 */
public class TreeUtil {

    /**
     * 根据反射获取属性值
     * @param t 实体对象
     * @param fieldName 属性名称
     */
    private static<T> Object getFieldValue(T t,String fieldName){
        Field field = ReflectUtil.getField(t.getClass(), fieldName);
        return ReflectUtil.getFieldValue(t, field);
    }

	/**
     * 获取树结构
     * @param list 数据
     */
    public static<T> List<T> buildTree(List<T> list) {
        return buildTree(list, "0","parentId");
    }

    /**
     * 获取树结构
     * @param list 数据
     * @param pId 父级id
     * @param fieldName 父级字段名称
     */
    public static<T> List<T> buildTree(List<T> list,String pId,String fieldName) {
        List<T> root= new ArrayList<>();
        Iterator<T> it = list.iterator();
        while (it.hasNext()) {
            T t = it.next();
            String parentId = getFieldValue(t,fieldName).toString();
            if (parentId.equals(pId)) {
                root.add(t);
                //it.remove();
            }
        }
        getChild(root, list,fieldName);
        return root;
    }

    /**
     * 递归子节点
     */
    public static<T> void getChild(List<T> root,List<T> list,String fileName){
        try {
            for (T nav : root) {
                String id = getFieldValue(nav,"id").toString();
                List<T> childList = buildTree(list, id,fileName);
                getChild(childList, list,fileName);
                Field child = ReflectUtil.getField(nav.getClass(), "children");
                child.set(nav,childList);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

最后

行政区划代码资源文件下载:

链接: 网盘下载 提取码: 5rp2
链接: csdn下载

你可能感兴趣的:(工具类,java,json,sql)