省市区 学习记录
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;}