@Test
public void saveDic() {
ResponseEntity<JSONObject> resp = restTemplate.getForEntity("https://restapi.amap.com/v3/config/district?keywords=532600&subdistrict=3&key=12b546d2fdaa9e3d828d79051f43abdc", JSONObject.class);
JSONObject dicJson = resp.getBody();
resoveDicTree(dicJson, 0);
}
/**
* 解析行政区域树
*
* @param jsonObject
*/
public void resoveDicTree(JSONObject jsonObject, int pid) {
JSONArray districts = jsonObject.getJSONArray("districts");
for (Object district : districts) {
JSONObject jsonObject1 = (JSONObject) district;
String name = jsonObject1.getString("name");
YdDistrict ydDistrictDto = new YdDistrict();
ydDistrictDto.setCreateTime(new Date());
ydDistrictDto.setParentId(pid);
ydDistrictDto.setDistrictName(name);
ydDistrictDto = ydDistrictService.insert(ydDistrictDto);
if (jsonObject1.getJSONArray("districts").size() > 0) {
Integer p = ydDistrictDto.getId();
resoveDicTree(jsonObject1, p);
} else {
resoveDicTree(jsonObject1, pid);
}
}
}