手记

Java 地区字典之省市区三级联动 (一)

Java 地区字典之省市区三级联动 (一)

我相信很多同学关于数据字典的问题困扰很久了,今天就讲述一下地区字典的构建。

移动端将 area.json 文件放到合适位置,主要考虑到客户体验方面。

服务端将文件放到D盘或合适的地方,读取文件

一、关于json文件的处理

/**
       * 解析 省市区 JSON
       *
       * @param area_json
       * @return
       */
      @RequestMapping(value=Route.System.FORMAT_AREA_JSON,method=RequestMethod.POST)
      @ResponseBody
      public Response format_area_json() {
            log.info("infoMsg:--- 解析 省市区 JSON开始");
            Response reponse = this.getReponse();
            String sname = ""; 
            // 读取nameID.txt文件中的NAMEID字段(key)对应值(value)并存储 
            try {
                  StringBuilder stringBuilder=new StringBuilder(); 
                  BufferedReader brname = new BufferedReader(new FileReader("D:/database/area.json"));
            while ((sname = brname.readLine()) != null) { 
                  stringBuilder.append(sname);        // 将对应value添加到链表存储 
            } 
            brname.close(); 
            System.out.println(stringBuilder);
                  if(null != stringBuilder) {
                        System.out.println(stringBuilder);
                  }
                  JSONArray object = JSON.parseArray(stringBuilder.toString());
                  for (Iterator provnce = object.iterator(); provnce.hasNext();) { 
                  JSONObject job = (JSONObject)provnce.next(); 
                  String name = job.get("name").toString(); 
                  String id = job.get("id").toString(); 
                  System.out.println(id + ":" + name);
                  SystemArea province = new SystemArea(id,name,"省");
                  systemEntityService.save(province);
                  JSONArray city = JSON.parseArray(job.get("city").toString()); 
                  for (Iterator citys = city.iterator(); citys.hasNext();) { 
                      JSONObject city_obj = (JSONObject)citys.next(); 
                      String city_name = city_obj.get("name").toString(); 
                      String city_id = city_obj.get("id").toString(); 
                      System.out.println(city_id + ":" + city_name); 
                      SystemArea cities = new SystemArea(city_id,city_name,"市");
                      systemEntityService.save(cities);
                      JSONArray area = JSON.parseArray(city_obj.get("area").toString()); 
                      for (Iterator areas = area.iterator(); areas.hasNext();) { 
                          JSONObject area_obj = (JSONObject)areas.next(); 
                          String area_name = area_obj.get("name").toString(); 
                          String area_id = area_obj.get("id").toString(); 
                          System.out.println(area_id + ":" + area_name); 
                          SystemArea arearry = new SystemArea(area_id,area_name,"地区");
                          systemEntityService.save(arearry);
                      }
                  }
              } 
                  log.info("infoMsg:--- 解析 省市区 JSON结束");
                  return reponse.success();
            } catch (Exception e) {
                  log.error("errorMsg:--- 解析 省市区 JSON 失败" + e.getMessage());
                  return reponse.failure(e.getMessage());
            }

      }

关于上述工具类,有俩点技术点

1. 一个是fastjson的使用,
2. 一个是I/O流的读取
1人推荐
随时随地看视频
慕课网APP