欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

2018版中国行政区代码和名称JSON  

程序员文章站 2022-06-04 15:22:37
...

1. 直接去*网站搞到代码列表

2. 去掉*,香港,澳门

3. 转成JSON

4. 可以用了

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

public class AreaCodeConvert {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {
		// 到*网站复制列表,保存到txt中,我找到的2016.3的
		// http://www.mca.gov.cn/article/sj/xzqh/2018/201804-12/201804-06041553.html
		// 直辖市和特别行政区比较讨厌,只有省的数据,没有市,或者没有区县,自己手动添加
		// 需要手动添加的有:北京,天津,上海,重庆,*,香港,澳门
		File f = new File("D:\\areaCode.txt");
		// 注意转码
		BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(f), "GBK"));
		String l = null;
		Map<String, Object> map = Maps.newLinkedHashMap();
		while ((l = r.readLine()) != null) {
			boolean isProvince = false, isCity = false, isRegion = false;
			// 左代码,右名称
			String[] split = StringUtils.split(l);
			String code = split[0];
			String name = split[1];
			// 代码规律:省级的都是xx0000,市级都是xxyy00
			isProvince = "0000".equals(StringUtils.substring(code, 2, 6));
			if (!isProvince) {
				isCity = "00".equals(StringUtils.substring(code, 4, 6));
			}
			if (!isCity) {
				isRegion = true;
			}
			if (isProvince) {
				// 存省级
				Map<String, Object> pMap = Maps.newLinkedHashMap();
				map.put(StringUtils.substring(code, 0, 2), pMap);
				pMap.put("code", code);
				pMap.put("name", name);
			} else if (isCity) {
				// 市级存到升级的children中
				Map<String, Object> pMap = (Map<String, Object>) map.get(StringUtils.substring(code, 0, 2));
				Map<String, Object> cMap = (Map<String, Object>) pMap.get("children");
				if (cMap == null) {
					cMap = Maps.newLinkedHashMap();
					pMap.put("children", cMap);
				}
				Map<String, Object> ccMap = Maps.newLinkedHashMap();
				cMap.put(StringUtils.substring(code, 0, 4), ccMap);
				ccMap.put("code", code);
				ccMap.put("pCode", StringUtils.substring(code, 0, 2) + "0000");
				ccMap.put("name", name);
			} else if (isRegion) {
				// 区级存到市级的children中
				System.out.println(code);
				Map<String, Object> pMap = (Map<String, Object>) map.get(StringUtils.substring(code, 0, 2));
				Map<String, Object> cMap = (Map<String, Object>) pMap.get("children");
				if (cMap == null) {
					String cCode = StringUtils.substring(code, 0, 4) + "00";
					cMap = Maps.newLinkedHashMap();
					Map<String, Object> ccMap = Maps.newLinkedHashMap();
					ccMap.put("code", cCode);
					ccMap.put("pCode", pMap.get("code"));
					ccMap.put("name", pMap.get("name"));
					cMap.put(cCode, ccMap);
					pMap.put("children", cMap);
				}
				Map<String, Object> ccMap = (Map<String, Object>) cMap.get(StringUtils.substring(code, 0, 4));
				// 坑爹的情况是有些是县级市,有些没有对应市的县,不过根据列表发现只是找到上一个就好了
				if (ccMap == null) {
					List<Entry<String, Object>> cList = new ArrayList<Entry<String, Object>>(cMap.entrySet());
					ccMap = (Map<String, Object>) cList.get(cList.size() - 1).getValue();
				}
				List<Map<String, Object>> rList = (List<Map<String, Object>>) ccMap.get("children");
				if (rList == null) {
					rList = Lists.newArrayList();
					ccMap.put("children", rList);
				}
				Map<String, Object> rMap = Maps.newLinkedHashMap();
				rMap.put("code", code);
				rMap.put("pCode", StringUtils.substring(code, 0, 4) + "00");
				rMap.put("name", name);
				rList.add(rMap);
			}
		}
		// Map不好看,转成List格式的
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>((Collection<? extends Map<String, Object>>) map.values());
		for (Map<String, Object> m : result) {
			Map<String, Object> c = (Map<String, Object>) m.get("children");
			m.put("children", new ArrayList<Map<String, Object>>((Collection<? extends Map<String, Object>>) c.values()));
		}
		ObjectMapper jsonMapper = new ObjectMapper();
		jsonMapper.setSerializationInclusion(Include.NON_DEFAULT);
		jsonMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
		System.out.println(jsonMapper.writeValueAsString(result));
		// 关闭,也懒得写try...catch了
		r.close();
	}
}