高德地图的json对应实体类
public class Amap {
private String status;
private String info;
private String infocode;
private String count;
private Suggestion suggestion;
private List<Districts> districts;
public void setStatus(String status) {
this.status = status;
}
public String getStatus() {
return status;
}
public void setInfo(String info) {
this.info = info;
}
public String getInfo() {
return info;
}
public void setInfocode(String infocode) {
this.infocode = infocode;
}
public String getInfocode() {
return infocode;
}
public void setCount(String count) {
this.count = count;
}
public String getCount() {
return count;
}
public void setSuggestion(Suggestion suggestion) {
this.suggestion = suggestion;
}
public Suggestion getSuggestion() {
return suggestion;
}
public void setDistricts(List<Districts> districts) {
this.districts = districts;
}
public List<Districts> getDistricts() {
return districts;
}
}
public class Suggestion {
private List<String> keywords;
private List<String> cities;
public void setKeywords(List<String> keywords) {
this.keywords = keywords;
}
public List<String> getKeywords() {
return keywords;
}
public void setCities(List<String> cities) {
this.cities = cities;
}
public List<String> getCities() {
return cities;
}
}
public class Districts {
private List<String> citycode;
private String adcode;
private String name;
private String center;
private String level;
private List<Districts> districts;
public void setCitycode(List<String> citycode) {
this.citycode = citycode;
}
public List<String> getCitycode() {
return citycode;
}
public void setAdcode(String adcode) {
this.adcode = adcode;
}
public String getAdcode() {
return adcode;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setCenter(String center) {
this.center = center;
}
public String getCenter() {
return center;
}
public void setLevel(String level) {
this.level = level;
}
public String getLevel() {
return level;
}
public void setDistricts(List<Districts> districts) {
this.districts = districts;
}
public List<Districts> getDistricts() {
return districts;
}
}
数据解析 插入数据
String data ="{\n" +
" \"status\":\"1\",\n" +
" \"info\":\"OK\",\n" +
" \"infocode\":\"10000\",\n" +
" \"count\":\"1\",\n" +
" \"suggestion\":{\n" +
" \"keywords\":[\n" +
"\n" +
" ],\n" +
" \"cities\":[\n" +
"\n" +
" ]\n" +
" },\n" +
" \"districts\":[\n" +
" {\n" +
" \"citycode\":[\n" +
"\n" +
" ],\n" +
" \"adcode\":\"530000\",\n" +
" \"name\":\"云南省\",\n" +
" \"center\":\"102.712251,25.040609\",\n" +
" \"level\":\"province\",\n" +
" \"districts\":[\n" +
" {\n" +
" \"citycode\":\"0870\",\n" +
" \"adcode\":\"530600\",\n" +
" \"name\":\"昭通市\",\n" +
" \"center\":\"103.717216,27.336999\",\n" +
" \"level\":\"city\",\n" +
" \"districts\":[\n" +
"\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"citycode\":\"0874\",\n" +
" \"adcode\":\"530300\",\n" +
" \"name\":\"曲靖市\",\n" +
" \"center\":\"103.797851,25.501557\",\n" +
" \"level\":\"city\",\n" +
" \"districts\":[\n" +
"\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"citycode\":\"0873\",\n" +
" \"adcode\":\"532500\",\n" +
" \"name\":\"红河哈尼族彝族自治州\",\n" +
" \"center\":\"103.384182,23.366775\",\n" +
" \"level\":\"city\",\n" +
" \"districts\":[\n" +
"\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"citycode\":\"0886\",\n" +
" \"adcode\":\"533300\",\n" +
" \"name\":\"怒江傈僳族自治州\",\n" +
" \"center\":\"98.854304,25.850949\",\n" +
" \"level\":\"city\",\n" +
" \"districts\":[\n" +
"\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"citycode\":\"0691\",\n" +
" \"adcode\":\"532800\",\n" +
" \"name\":\"西双版纳傣族自治州\",\n" +
" \"center\":\"100.797941,22.001724\",\n" +
" \"level\":\"city\",\n" +
" \"districts\":[\n" +
"\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"citycode\":\"0877\",\n" +
" \"adcode\":\"530400\",\n" +
" \"name\":\"玉溪市\",\n" +
" \"center\":\"102.543907,24.350461\",\n" +
" \"level\":\"city\",\n" +
" \"districts\":[\n" +
"\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"citycode\":\"0872\",\n" +
" \"adcode\":\"532900\",\n" +
" \"name\":\"大理白族自治州\",\n" +
" \"center\":\"100.225668,25.589449\",\n" +
" \"level\":\"city\",\n" +
" \"districts\":[\n" +
"\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"citycode\":\"0888\",\n" +
" \"adcode\":\"530700\",\n" +
" \"name\":\"丽江市\",\n" +
" \"center\":\"100.233026,26.872108\",\n" +
" \"level\":\"city\",\n" +
" \"districts\":[\n" +
"\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"citycode\":\"0876\",\n" +
" \"adcode\":\"532600\",\n" +
" \"name\":\"文山壮族苗族自治州\",\n" +
" \"center\":\"104.24401,23.36951\",\n" +
" \"level\":\"city\",\n" +
" \"districts\":[\n" +
"\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"citycode\":\"0887\",\n" +
" \"adcode\":\"533400\",\n" +
" \"name\":\"迪庆藏族自治州\",\n" +
" \"center\":\"99.706463,27.826853\",\n" +
" \"level\":\"city\",\n" +
" \"districts\":[\n" +
"\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"citycode\":\"0875\",\n" +
" \"adcode\":\"530500\",\n" +
" \"name\":\"保山市\",\n" +
" \"center\":\"99.167133,25.111802\",\n" +
" \"level\":\"city\",\n" +
" \"districts\":[\n" +
"\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"citycode\":\"0879\",\n" +
" \"adcode\":\"530800\",\n" +
" \"name\":\"普洱市\",\n" +
" \"center\":\"100.972344,22.777321\",\n" +
" \"level\":\"city\",\n" +
" \"districts\":[\n" +
"\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"citycode\":\"0871\",\n" +
" \"adcode\":\"530100\",\n" +
" \"name\":\"昆明市\",\n" +
" \"center\":\"102.712251,25.040609\",\n" +
" \"level\":\"city\",\n" +
" \"districts\":[\n" +
"\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"citycode\":\"0878\",\n" +
" \"adcode\":\"532300\",\n" +
" \"name\":\"楚雄彝族自治州\",\n" +
" \"center\":\"101.546046,25.041988\",\n" +
" \"level\":\"city\",\n" +
" \"districts\":[\n" +
"\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"citycode\":\"0883\",\n" +
" \"adcode\":\"530900\",\n" +
" \"name\":\"临沧市\",\n" +
" \"center\":\"100.08697,23.886567\",\n" +
" \"level\":\"city\",\n" +
" \"districts\":[\n" +
"\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"citycode\":\"0692\",\n" +
" \"adcode\":\"533100\",\n" +
" \"name\":\"德宏傣族景颇族自治州\",\n" +
" \"center\":\"98.578363,24.436694\",\n" +
" \"level\":\"city\",\n" +
" \"districts\":[\n" +
"\n" +
" ]\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
"}";
Amap amap = JSON.parseObject(data,new TypeReference<Amap>() {});
List<Area> list = new ArrayList<>();
amap.getDistricts().forEach(districts -> {
Area area =new Area();
area.setId("10000");
area.setParentId("0");
area.setAdcode(districts.getAdcode());
area.setName(districts.getName());
area.setLevel(districts.getLevel());
list.add(area);
if(districts.getDistricts()!=null && districts.getDistricts().size()>0){
districts.getDistricts().forEach(d->{
Area a =new Area();
a.setId(IdGen.uuid());
a.setParentId(area.getId());
a.setAncestors("0,"+area.getId());
a.setCitycode(d.getCitycode().get(0));
a.setAdcode(d.getAdcode());
a.setName(d.getName());
a.setLevel(d.getLevel());
list.add(a);
});
}
});
list.forEach(area -> {
this.areaService.insertArea(area);
});
这个方法 只有两次 数据
如果是4层 如何递归 所有数据 保存到 数据库呢