`
wx1569063608
  • 浏览: 22724 次
文章分类
社区版块
存档分类
最新评论

中华人民共和国县以上行政区划代码转JSON

 
阅读更多

处理 区域代码,转成json格式,例如2017年最新的区域代码http://www.mca.gov.cn/article/sj/tjbz/a/2017/20178/201709251028.html

注意4大直辖市处理不了,需要手动增加一行例如110100    北京市。

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;
import com.ysccc.spring.boot.tools.Emptys;

public class AreaCodeConvert {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {
		// 到政府网站复制列表,保存到txt中,我找到的2017年的
		// http://www.mca.gov.cn/article/sj/tjbz/a/2017/20178/201709251028.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中
				Map<String, Object> pMap = (Map<String, Object>) map.get(StringUtils.substring(code, 0, 2));
				Map<String, Object> cMap = (Map<String, Object>) pMap.get("children");
				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");
			if (Emptys.isNotEmpty(c)) {// 台湾、香港、澳门就一个地方
				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();
	}
}

 

转载于:https://my.oschina.net/u/1261213/blog/1551456

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics