省市JSON
[{
"name": "北京市",
"code": "110000",
"city": [{
"name": "市辖区",
"code": "110100",
"area": [{
"name": "东城区",
"code": "110101"
},
{
"name": "西城区",
"code": "110102"
},
{
"name": "朝阳区",
"code": "110105"
},
{
"name": "丰台区",
"code": "110106"
},
{
"name": "石景山区",
"code": "110107"
},
{
"name": "海淀区",
"code": "110108"
},
{
"name": "门头沟区",
"code": "110109"
},
{
"name": "房山区",
"code": "110111"
},
{
"name": "通州区",
"code": "110112"
},
{
"name": "顺义区",
"code": "110113"
},
{
"name": "昌平区",
"code": "110114"
},
{
"name": "大兴区",
"code": "110115"
},
{
"name": "怀柔区",
"code": "110116"
},
{
"name": "平谷区",
"code": "110117"
},
{
"name": "密云区",
"code": "110118"
},
{
"name": "延庆区",
"code": "110119"
}]
}]
},
{
"name": "江苏省",
"code": "320000",
"city": [{
"name": "南京市",
"code": "320100",
"area": [{
"name": "市辖区",
"code": "320101"
},
{
"name": "玄武区",
"code": "320102"
},
{
"name": "秦淮区",
"code": "320104"
},
{
"name": "建邺区",
"code": "320105"
},
{
"name": "鼓楼区",
"code": "320106"
},
{
"name": "浦口区",
"code": "320111"
},
{
"name": "栖霞区",
"code": "320113"
},
{
"name": "雨花台区",
"code": "320114"
},
{
"name": "江宁区",
"code": "320115"
},
{
"name": "六合区",
"code": "320116"
},
{
"name": "溧水区",
"code": "320117"
},
{
"name": "高淳区",
"code": "320118"
}]
},
{
"name": "无锡市",
"code": "320200",
"area": [{
"name": "市辖区",
"code": "320201"
},
{
"name": "锡山区",
"code": "320205"
},
{
"name": "惠山区",
"code": "320206"
},
{
"name": "滨湖区",
"code": "320211"
},
{
"name": "梁溪区",
"code": "320213"
},
{
"name": "新吴区",
"code": "320214"
},
{
"name": "江阴市",
"code": "320281"
},
{
"name": "宜兴市",
"code": "320282"
}]
},
{
"name": "徐州市",
"code": "320300",
"area": [{
"name": "市辖区",
"code": "320301"
},
{
"name": "鼓楼区",
"code": "320302"
},
{
"name": "云龙区",
"code": "320303"
},
{
"name": "贾汪区",
"code": "320305"
},
{
"name": "泉山区",
"code": "320311"
},
{
"name": "铜山区",
"code": "320312"
},
{
"name": "丰县",
"code": "320321"
},
{
"name": "沛县",
"code": "320322"
},
{
"name": "睢宁县",
"code": "320324"
},
{
"name": "新沂市",
"code": "320381"
},
{
"name": "邳州市",
"code": "320382"
}]
},
{
"name": "常州市",
"code": "320400",
"area": [{
"name": "市辖区",
"code": "320401"
},
{
"name": "天宁区",
"code": "320402"
},
{
"name": "钟楼区",
"code": "320404"
},
{
"name": "新北区",
"code": "320411"
},
{
"name": "武进区",
"code": "320412"
},
{
"name": "金坛区",
"code": "320413"
},
{
"name": "溧阳市",
"code": "320481"
}]
},
{
"name": "苏州市",
"code": "320500",
"area": [{
"name": "市辖区",
"code": "320501"
},
{
"name": "虎丘区",
"code": "320505"
},
{
"name": "吴中区",
"code": "320506"
},
{
"name": "相城区",
"code": "320507"
},
{
"name": "姑苏区",
"code": "320508"
},
{
"name": "吴江区",
"code": "320509"
},
{
"name": "常熟市",
"code": "320581"
},
{
"name": "张家港市",
"code": "320582"
},
{
"name": "昆山市",
"code": "320583"
},
{
"name": "太仓市",
"code": "320585"
}]
},
{
"name": "南通市",
"code": "320600",
"area": [{
"name": "市辖区",
"code": "320601"
},
{
"name": "崇川区",
"code": "320602"
},
{
"name": "港闸区",
"code": "320611"
},
{
"name": "通州区",
"code": "320612"
},
{
"name": "海安县",
"code": "320621"
},
{
"name": "如东县",
"code": "320623"
},
{
"name": "启东市",
"code": "320681"
},
{
"name": "如皋市",
"code": "320682"
},
{
"name": "海门市",
"code": "320684"
}]
},
{
"name": "连云港市",
"code": "320700",
"area": [{
"name": "市辖区",
"code": "320701"
},
{
"name": "连云区",
"code": "320703"
},
{
"name": "海州区",
"code": "320706"
},
{
"name": "赣榆区",
"code": "320707"
},
{
"name": "东海县",
"code": "320722"
},
{
"name": "灌云县",
"code": "320723"
},
{
"name": "灌南县",
"code": "320724"
}]
},
{
"name": "淮安市",
"code": "320800",
"area": [{
"name": "市辖区",
"code": "320801"
},
{
"name": "淮安区",
"code": "320803"
},
{
"name": "淮阴区",
"code": "320804"
},
{
"name": "清江浦区",
"code": "320812"
},
{
"name": "洪泽区",
"code": "320813"
},
{
"name": "涟水县",
"code": "320826"
},
{
"name": "盱眙县",
"code": "320830"
},
{
"name": "金湖县",
"code": "320831"
}]
},
{
"name": "盐城市",
"code": "320900",
"area": [{
"name": "市辖区",
"code": "320901"
},
{
"name": "亭湖区",
"code": "320902"
},
{
"name": "盐都区",
"code": "320903"
},
{
"name": "大丰区",
"code": "320904"
},
{
"name": "响水县",
"code": "320921"
},
{
"name": "滨海县",
"code": "320922"
},
{
"name": "阜宁县",
"code": "320923"
},
{
"name": "射阳县",
"code": "320924"
},
{
"name": "建湖县",
"code": "320925"
},
{
"name": "东台市",
"code": "320981"
}]
},
{
"name": "扬州市",
"code": "321000",
"area": [{
"name": "市辖区",
"code": "321001"
},
{
"name": "广陵区",
"code": "321002"
},
{
"name": "邗江区",
"code": "321003"
},
{
"name": "江都区",
"code": "321012"
},
{
"name": "宝应县",
"code": "321023"
},
{
"name": "仪征市",
"code": "321081"
},
{
"name": "高邮市",
"code": "321084"
}]
},
{
"name": "镇江市",
"code": "321100",
"area": [{
"name": "市辖区",
"code": "321101"
},
{
"name": "京口区",
"code": "321102"
},
{
"name": "润州区",
"code": "321111"
},
{
"name": "丹徒区",
"code": "321112"
},
{
"name": "丹阳市",
"code": "321181"
},
{
"name": "扬中市",
"code": "321182"
},
{
"name": "句容市",
"code": "321183"
}]
},
{
"name": "泰州市",
"code": "321200",
"area": [{
"name": "市辖区",
"code": "321201"
},
{
"name": "海陵区",
"code": "321202"
},
{
"name": "高港区",
"code": "321203"
},
{
"name": "姜堰区",
"code": "321204"
},
{
"name": "兴化市",
"code": "321281"
},
{
"name": "靖江市",
"code": "321282"
},
{
"name": "泰兴市",
"code": "321283"
}]
},
{
"name": "宿迁市",
"code": "321300",
"area": [{
"name": "市辖区",
"code": "321301"
},
{
"name": "宿城区",
"code": "321302"
},
{
"name": "宿豫区",
"code": "321311"
},
{
"name": "沭阳县",
"code": "321322"
},
{
"name": "泗阳县",
"code": "321323"
},
{
"name": "泗洪县",
"code": "321324"
}]
}]
},
]
AdministrativeDivisionUtil
package com.vipsoft.base.util;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import org.aspectj.util.FileUtil;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@Component
public class AdministrativeDivisionUtil {
private String cityJsonPath = "/config/city_code_cn.json";
public String getProvinceCode(String provinceName) {
try {
String jsonPath = this.getClass().getResource(cityJsonPath).getPath();
String jsonString = FileUtil.readAsString(new File(jsonPath));
List<Map<String, String>> map = JSON.parseObject(jsonString, List.class);
for (Map<String, String> province : map) {
if (province.get("name").contains(provinceName)) {
return province.get("code");
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return "";
}
public String getCityCode(String provinceName, String cityName) {
try {
String jsonPath = this.getClass().getResource(cityJsonPath).getPath();
String jsonString = FileUtil.readAsString(new File(jsonPath));
List<Map<String, String>> map = JSON.parseObject(jsonString, List.class);
String code = "";
for (Map<String, String> province : map) {
if (!province.get("name").contains(provinceName)) {
continue;
}
Object cityJson = province.get("city");
if (cityJson != null) {
String jsonStr = JSON.toJSONString(cityJson);
JSONArray jsonArray = JSON.parseArray(jsonStr);
List<Map> javaList = jsonArray.toJavaList(Map.class);
for (Map<String, String> city : javaList) {
if (city.get("name").contains(cityName)) {
return city.get("code");
}
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return "";
}
}
标签:编码,code,name,area,市辖区,获取,名称,import,String
From: https://www.cnblogs.com/vipsoft/p/18596875