当前位置: 首页 > news >正文

生成省市区JSON

省市区 学习记录

https://xiangyuecn.github.io/AreaCity-JsSpider-StatsGov/

package cn.serverx.sx.your;

import cn.hutool.core.io.FileUtil;
import cn.serverx.sx.your.vo.DistrictNode;
import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.nio.charset.Charset;
import java.util.List;

public class TempCityJsonHandler {

    public static void main(String[] args) throws FileNotFoundException {
        RandomAccessFile accessFile = new RandomAccessFile("D:\\workspace\\blzcgl-backend\\sx-admin\\src\\main\\resources\\static\\city.json", "r");

        StringBuffer buffer = new StringBuffer();
        String line = null;
        while ((line = FileUtil.readLine(accessFile, Charset.defaultCharset())) != null) {
            buffer.append(line.trim());
        }

        List<DistrictNode> nodeList = JSON.parseArray(buffer.toString(), DistrictNode.class);

        List<DistrictNode> newNodeList = Lists.newArrayList();

        List<String> checkCode = Lists.newArrayList();
        for (DistrictNode provinceNode : nodeList) {
            if (checkCode.contains(provinceNode.getCode())) {
                throw new RuntimeException("重复code:" + provinceNode.getCode());
            } else {
                checkCode.add(provinceNode.getCode());
            }
            DistrictNode newPro = new DistrictNode();
            newPro.setCode(provinceNode.getCode());
            newPro.setName(provinceNode.getName());
            newPro.setChildList(Lists.newArrayList());
            newNodeList.add(newPro);

            List<DistrictNode> cityNodes = provinceNode.getChildList();
            int totalCityNode = cityNodes.size();
            int cityIndex = 1;

            for (DistrictNode cityNode : cityNodes) {
                if (checkCode.contains(cityNode.getCode())) {
                    throw new RuntimeException("重复code:" + cityNode.getCode());
                } else {
                    checkCode.add(cityNode.getCode());
                }
                DistrictNode newCity = new DistrictNode();
                newCity.setCode(cityNode.getCode());
                newCity.setName(cityNode.getName());
                newCity.setChildList(Lists.newArrayList());
                newPro.getChildList().add(newCity);

                List<DistrictNode> areaNodes = cityNode.getChildList();
                int totalAreaNode = areaNodes.size();
                int areaIndex = 1;

                for (DistrictNode areaNode : areaNodes) {
                    if (checkCode.contains(areaNode.getCode())) {
                        throw new RuntimeException("重复code:" + areaNode.getCode());
                    } else {
                        checkCode.add(areaNode.getCode());
                    }
                    DistrictNode newArea = new DistrictNode();
                    newArea.setCode(areaNode.getCode());
                    newArea.setName(areaNode.getName());
                    newCity.getChildList().add(newArea);

                    if (areaIndex++ == totalAreaNode) {
                        DistrictNode aNode = new DistrictNode();
                        aNode.setName("其他");
                        String code = StringUtils.substring(areaNode.getCode(), 0, 4);
                        aNode.setCode(code + "99");
                        newCity.getChildList().add(aNode);
                    }
                }

                if (cityIndex++ == totalCityNode) {
                    DistrictNode cNode = new DistrictNode();
                    cNode.setName("其他");
                    String code = StringUtils.substring(cityNode.getCode(), 0, 2);
                    cNode.setCode(code + "9900");

                    DistrictNode aNode = new DistrictNode();
                    aNode.setName("其他");
                    aNode.setCode(code + "9999");
                    cNode.setChildList(Lists.newArrayList(aNode));

                    newPro.getChildList().add(cNode);
                }
            }

        }
        System.out.println(JSON.toJSONString(checkCode));
        System.out.println(JSON.toJSONString(newNodeList));
        FileUtil.writeString(JSON.toJSONString(newNodeList), new File("D:\\data\\district.json"), Charset.defaultCharset());
    }
}


package cn.serverx.sx.your.vo;

import com.alibaba.fastjson2.annotation.JSONField;
import lombok.Data;

import java.util.List;

@Data
public class DistrictNode {

    @JSONField(name = "Name")
    private String name;

    @JSONField(name = "Code")
    private String code;

    private String parentCode;

    @JSONField(name = "ChildList")
    private List<DistrictNode> childList;

}


相关文章:

  • http 405 Not Allowed
  • 2018年全国职业院校技能大赛-高职组计算机网络应用竞赛竞赛样题A卷
  • 一文讲通锁标记对象std::adopt_lock盲点
  • OpenAI与谷歌DeepMind新品同日竞技,谁能引领机器人现实任务新潮流?
  • C#-委托delegate
  • C++设计模式-观察者模式:从基本介绍,内部原理、应用场景、使用方法,常见问题和解决方案进行深度解析
  • 网络视频监控平台在医疗领域的应用
  • 浏览器中输入 URL 到显示主页的完整过程
  • 【后端】【django】Django 自带的用户系统与 RBAC 机制
  • 历次科技泡沫对人工智能发展的启示与规避措施
  • containerd 拉取镜像的工具以及优劣
  • Python----计算机视觉处理(opencv:图片灰度化)
  • go 安装swagger
  • 【论文精读】Deformable DETR:用于端到端目标检测可变形 Transformer
  • go 加载yaml配置文件
  • 3-1 写分享报告
  • 鸿蒙编译框架插件HvigorPlugin接口的用法介绍
  • zuul路由转发功能的核心流程
  • 【docker系】docker安装数据迁移
  • 【含文档+PPT+源码】基于Django框架的乡村绿色农产品交易平台的设计与实现
  • 印巴战火LIVE丨“快速接近战争状态”:印度袭击巴军事基地,巴启动反制军事行动
  • 湖南省职业病防治院通报3岁女童确诊“铊中毒”:去年病例,编辑误写为“近日”
  • 美乌基金协议:美国搞了一套可在资源富集地区复刻的商业模式
  • 阿里CEO:将以饱和式投入打法,聚焦几大核心战役
  • 第一集|《刑警的日子》很生活,《执法者们》有班味
  • 陕西永寿4岁女童被蜜蜂蜇伤致死,当地镇政府介入处理