一,从数据库查出来的数据
两张表先各自左外连接,然后在相互左外连接查找省市县的数据(业务需求必须这样做,省市去的是第一张表,而市县取的是第二张表,两张表中间通过市的名字连接)见这个博文的最后一张图片
二,要求的json格式的数据
三,实现的关键代码
1 public String queryFullZoneInfo() {
2 List<Map<String, String>> mapList = zoneInfoService.queryFullZoneInfo();
3 List<Map<String, Object>> zoneList = new ArrayList<Map<String, Object>>();
4 Map<String, Object> existMap = new HashMap<String, Object>();
5 for (Map<String, String> map : mapList) {
6 String province = map.get("province");
7 Map<String, Object> provinceMap = null;
8 if (existMap.containsKey(province)) {
9 provinceMap = (Map<String, Object>) existMap.get(province);
10 } else {
11 provinceMap = new HashMap<String, Object>();
12 existMap.put(province, provinceMap);
13 zoneList.add(provinceMap);
14 }
15
16 provinceMap.put("Code", map.get("provinceCode"));
17 provinceMap.put("Name", map.get("province"));
18 String city = map.get("city");
19 if (StringUtils.isNotEmpty(city)) {
20 List<Map<String, Object>> provinceCityList = null;
21 if (existMap.containsKey(province + "_")) {
22 provinceCityList = (List<Map<String, Object>>) existMap.get(province + "_");
23 } else {
24 provinceCityList = new ArrayList<Map<String, Object>>();
25 existMap.put(province + "_", provinceCityList);
26 provinceMap.put("level", provinceCityList);
27 }
28 Map<String, Object> provinceCityMap = null;
29 if (existMap.containsKey(province + "_" + city)) {
30 provinceCityMap = (Map<String, Object>) existMap.get(province + "_" + city);
31 } else {
32 provinceCityMap = new HashMap<String, Object>();
33 existMap.put(province + "_" + city, provinceCityMap);
34 provinceCityList.add(provinceCityMap);
35 String citySort = String.valueOf(existMap.get(province + "_sort"));
36 existMap.put(province + "_sort", "null".equals(citySort) ? 1 : NumberUtils.toInt(citySort) + 1);
37 provinceCityMap.put("sort", existMap.get(province + "_sort"));
38 }
39
40 provinceCityMap.put("Name", city);
41 provinceCityMap.put("Code", map.get("cityCode"));
42
43 String country = map.get("country");
44 if (StringUtils.isNotEmpty(country)) {
45 List<Map<String, Object>> provinceCityCountryList = null;
46 if (existMap.containsKey(province + "_" + city + "_")) {
47 provinceCityCountryList = (List<Map<String, Object>>) existMap.get(province + "_" + city + "_");
48 } else {
49 provinceCityCountryList = new ArrayList<Map<String, Object>>();
50 existMap.put(province + "_" + city + "_", provinceCityCountryList);
51 provinceCityMap.put("level", provinceCityCountryList);
52 }
53
54 Map<String, Object> provinceCityCountryMap = null;
55 if (existMap.containsKey(province + "_" + city + "_" + country)) {
56 provinceCityCountryMap = (Map<String, Object>) existMap.get(province + "_" + city + "_" + country);
57 } else {
58 provinceCityCountryMap = new HashMap<String, Object>();
59 existMap.put(province + "_" + city + "_" + country, provinceCityCountryMap);
60 provinceCityCountryList.add(provinceCityCountryMap);
61 String countrySort = String.valueOf(existMap.get(province + "_" + city + "_sort"));
62 existMap.put(province + "_" + city + "_sort", "null".equals(countrySort) ? 1 : NumberUtils.toInt(countrySort) + 1);
63 provinceCityCountryMap.put("sort", existMap.get(province + "_" + city + "_sort"));
64 }
65
66 provinceCityCountryMap.put("Code", map.get("countryCode"));
67 provinceCityCountryMap.put("Name", country);
68 }
69 }
70 }
71
72 try {
73 return JsonUtils.toJson(zoneList);
74 } catch (Exception e) {
75 e.printStackTrace();
76 }
77
78 return null;
79 }
80