java代码把行政区划代码转json格式及sql

数据来源:中华人民共和国民政部
直接上代码:

import com.alibaba.fastjson.JSON;

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

public class ZoneCodeUtils {
    public static void main(String[] args) throws IOException {
        String inPathName = "C:\\Users\\WBT\\Desktop\\in.txt";
        String jsonPathName = "C:\\Users\\WBT\\Desktop\\out.json";
        String sqlPathName = "C:\\Users\\WBT\\Desktop\\out.sql";
        // 读取行政区划列表,并输出json格式文件
        convertToJsonFile(inPathName, jsonPathName);
        // 读取行政区划列表,并输出sql文件
        convertToSqlFile(inPathName, sqlPathName);
        // 读取行政区划列表,并输出json格式文件(对四个直辖市,无区县的市,及港澳台补全三级)
        // convertToJsonFile2(inPathName, jsonPathName);
    }

    private static void convertToJsonFile(String inPathName, String outPathName) throws IOException {
        // 读取行政区划代码文件
        File inFile = new File(inPathName);
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inFile), "GBK"));
        String line = null;
        // 省级区划集合
        List<Zone> provinceList = new ArrayList<>();
        Zone provinceZone = new Zone();
        // 市级区划集合
        List<Zone> cityList = new ArrayList<>();
        Zone cityZone = new Zone();
        // 县级区划集合
        List<Zone> countyList = new ArrayList<>();
        Zone countyZone = new Zone();
        while ((line = reader.readLine()) != null) {
            String newLine = line.trim();
            if (newLine.length() < 6) {
                continue;
            }
            String code = newLine.substring(0, 6);
            // 如果前6位不能转为数据,进行下一次循环
            try {
                Integer.parseInt(code);
            } catch (NumberFormatException e) {
                continue;
            }
            String name = newLine.substring(7).trim();
            // 省级
            if (code.endsWith("0000")) {
                provinceZone = new Zone(code, name);
                provinceList.add(provinceZone);
                cityList = new ArrayList<>();
                continue;
            }
            // 市级(北京、天津、上海、重庆下的县区当市处理)
            if (code.endsWith("00") || code.startsWith("11") || code.startsWith("12")
                    || code.startsWith("31") || code.startsWith("50")) {
                cityZone = new Zone(code, name);
                cityList.add(cityZone);
                provinceZone.setChildren(cityList);
                countyList = new ArrayList<>();
                continue;
            }
            // 县级
            countyZone = new Zone(code, name);
            countyList.add(countyZone);
            cityZone.setChildren(countyList);
        }
        reader.close();
        String result = JSON.toJSON(provinceList).toString();
        // 将json文件输出到本地
        convertTextToLocalFile(outPathName, result);
    }

    private static void convertToJsonFile2(String inPathName, String outPathName) throws IOException {
        // 读取行政区划代码文件
        File inFile = new File(inPathName);
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inFile), "GBK"));
        String line = null;
        // 省级区划集合
        List<Zone> provinceList = new ArrayList<>();
        Zone provinceZone = new Zone();
        // 市级区划集合
        List<Zone> cityList = new ArrayList<>();
        Zone cityZone = new Zone();
        // 县级区划集合
        List<Zone> countyList = new ArrayList<>();
        Zone countyZone = new Zone();
        // 上一次循环的区划代码
        String lastCode = "";
        while ((line = reader.readLine()) != null) {
            String newLine = line.trim();
            if (newLine.length() < 6) {
                continue;
            }
            String code = newLine.substring(0, 6);
            // 如果前6位不能转为数据,进行下一次循环
            try {
                Integer.parseInt(code);
            } catch (NumberFormatException e) {
                continue;
            }
            String name = newLine.substring(7).trim();
            // 特殊市的处理,补全三级(三沙市,东莞市,中山市等)
            if ((code.endsWith("0000") || code.endsWith("00")) && lastCode.endsWith("00") && !lastCode.endsWith("0000")) {
                List<Zone> tempCityZoneList = provinceList.get(provinceList.size() - 1).getChildren();
                Zone tempCityZone = tempCityZoneList.get(tempCityZoneList.size() - 1);
                List<Zone> tempCountyZoneList = new ArrayList<>();
                tempCountyZoneList.add(new Zone(tempCityZone.getCode(), tempCityZone.getName()));
                tempCityZone.setChildren(tempCountyZoneList);
            }
            lastCode = code;
            // 港澳台的处理,补全三级
            if ("710000".equals(code) || "810000".equals(code) || "820000".equals(code)) {
                Zone tempCountyZone = new Zone(code, name);
                List<Zone> tempCountyZoneList = new ArrayList<>();
                tempCountyZoneList.add(tempCountyZone);
                Zone tempCityZone = new Zone(code, name, tempCountyZoneList);
                List<Zone> tempCityZoneList = new ArrayList<>();
                tempCityZoneList.add(tempCityZone);
                provinceZone = new Zone(code, name, tempCityZoneList);
                provinceList.add(provinceZone);
                cityList = new ArrayList<>();
                continue;
            }
            // 省级
            if (code.endsWith("0000")) {
                provinceZone = new Zone(code, name);
                provinceList.add(provinceZone);
                cityList = new ArrayList<>();
                continue;
            }
            // 市级
            if (code.endsWith("00")) {
                cityZone = new Zone(code, name);
                cityList.add(cityZone);
                provinceZone.setChildren(cityList);
                countyList = new ArrayList<>();
                continue;
            }
            // 北京、天津、上海、重庆下的县区当市处理,并补全第三级
            if (code.startsWith("11") || code.startsWith("12")
                    || code.startsWith("31") || code.startsWith("50")) {
                List<Zone> tempList = new ArrayList<>();
                tempList.add(new Zone(code, name));
                cityZone = new Zone(code, name, tempList);
                cityList.add(cityZone);
                provinceZone.setChildren(cityList);
                countyList = new ArrayList<>();
                continue;
            }
            // 县级
            countyZone = new Zone(code, name);
            countyList.add(countyZone);
            cityZone.setChildren(countyList);
        }
        reader.close();
        String result = JSON.toJSON(provinceList).toString();
        // 将json文件输出到本地
        convertTextToLocalFile(outPathName, result);
    }

    private static void convertToSqlFile(String inPathName, String outPathName) throws IOException {
        // 读取行政区划代码文件
        File file = new File(inPathName);
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "GBK"));
        String line = null;
        StringBuilder builder = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            String newLine = line.trim();
            if (newLine.length() < 6) {
                continue;
            }
            String code = newLine.substring(0, 6);
            // 如果前6位不能转为数据,进行下一次循环
            try {
                Integer.parseInt(code);
            } catch (NumberFormatException e) {
                continue;
            }

            String name = newLine.substring(7).trim();
            // 省级
            if (code.endsWith("0000")) {
                builder.append("insert into zone_code (parent, code, name) value ('0', '").append(code)
                        .append("', '").append(name).append("');").append("\r\n");
                continue;
            }
            // 市级(北京、天津、上海、重庆下的县区当市处理)
            if (code.endsWith("00") || code.startsWith("11") || code.startsWith("12")
                    || code.startsWith("31") || code.startsWith("50")) {
                String parentCode = code.substring(0, 2) + "0000";
                builder.append("insert into zone_code (parent, code, name) value ('").append(parentCode)
                        .append("', '").append(code).append("', '").append(name).append("');").append("\r\n");
                continue;
            }
            // 县级
            String parentCode = code.substring(0, 4) + "00";
            builder.append("insert into zone_code (parent, code, name) value ('").append(parentCode)
                    .append("', '").append(code).append("', '").append(name).append("');").append("\r\n");
        }
        reader.close();
        // 将sql文件输出到本地
        convertTextToLocalFile(outPathName, builder.toString());
    }

    private static void convertTextToLocalFile(String fileName, String text) throws IOException {
        File file = new File(fileName);
        // 如果文件不存在则创建文件
        if (!file.exists()) {
            file.createNewFile();
        }
        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
        // 将字符串写到文件
        outputStream.write(text.getBytes());
        outputStream.flush();
        outputStream.close();
    }

    static class Zone {
        private String code;
        private String name;
        private List<Zone> children;

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<Zone> getChildren() {
            return children;
        }

        public void setChildren(List<Zone> children) {
            this.children = children;
        }

        public Zone() {
            super();
        }

        public Zone(String code, String name) {
            this.code = code;
            this.name = name;
        }

        public Zone(String code, String name, List<Zone> children) {
            this.code = code;
            this.name = name;
            this.children = children;
        }
    }
}

总结一下,写的不是很好,但是完全够用,毕竟只是用一次,又不是放在系统中天天用。
之所以自己写是因为网上找的基本不符合我的需求,或者代码引用了太多的第三方依赖,很头疼。我只是引了一个alibaba的json转换工具包,别的都是java的包。当然不一定用fastjson工具包,只要能把对象转json就行,然后自己稍微改下报错的代码。
sql的话,只是举个例子,根据自己数据库表的实际情况进行修改。
原数据只支持官方的数据源,如下图格式。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值