当前位置: 首页 > 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://www.dtcms.com/a/69699.html

相关文章:

  • 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框架的乡村绿色农产品交易平台的设计与实现
  • 论文分享:PL-ALF框架实现无人机低纹理环境自主飞行
  • KICK第五课:Mac 系统下安装 Xcode 或 Clang
  • BGP路由聚合
  • 开发小运维-搭建rocketmq集群
  • OpenWebUI项目调研对比
  • Windows远程桌面黑屏怎么办?
  • 如何限制非法的WIFI连接操作以及网络端口使用?
  • 【每日学点HarmonyOS Next知识】状态栏字体、生命周期、自定义对话框屏幕中间、透明度、tab居中
  • 六十天前端强化训练之第十六天JSX语法深度解析与应用实践指南
  • 笔记:基于springboot+ShardingSphere-jdbc5.0.0的分库分表(偏yml配置)