栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;

先看看这几份数据:

1、data.json

2、demo.json

3、source.json

4、target.json

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

6、targetSql.json

数据处理
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 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 children = new ArrayList<>();
    }

	
    private static void exportTxt(List 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();
    }

    
    private static void exportExcel(List dataList) throws IOException {
        List> rowAll = new ArrayList>();
        List 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 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();
    }

    
    private static void exportJson(List dataList) throws IOException {
    	// 输出的json需要有层级结构,所以需要将dataList构建为树结构
        List 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 list = readSourceTxt();
        List dataList = new ArrayList<>();
        int id = 1;
        String pid="0";
        getCityList(list,dataList,id,pid);
        // 生成txt文件
        exportTxt(dataList);
        // 生成excel
        exportExcel(dataList);
        // 生成json文件
        exportJson(dataList);
    }

    
    private static List 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);
    }

    
    public static int getCityList(List list,List 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.replace("00","");
                city.setLevel(2);
            }else {
                city.setLevel(3);
            }
            //code只取最后两位,完整的编码前面几位都是由它的父级组成的,intactCode存储的是完整的编码
            city.setCode(code.substring(code.length()-2));
            city.setPid(pid);
            dataList.add(city);
            id++;
            List children = entity.getChildren();
            if (children != null && !children.isEmpty()){
                id = getCityList(children,dataList,id, city.getId());
            }
        }
        return id;
    }
}
public class TreeUtil {

    
    private static Object getFieldValue(T t,String fieldName){
        Field field = ReflectUtil.getField(t.getClass(), fieldName);
        return ReflectUtil.getFieldValue(t, field);
    }

	
    public static List buildTree(List list) {
        return buildTree(list, "0","parentId");
    }

    
    public static List buildTree(List list,String pId,String fieldName) {
        List root= new ArrayList<>();
        Iterator 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 void getChild(List root,List list,String fileName){
        try {
            for (T nav : root) {
                String id = getFieldValue(nav,"id").toString();
                List 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();
        }
    }
}
最后

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

链接: 网盘下载 提取码: yxpr
链接: csdn下载

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/946322.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号