NutzCN Logo
问答 Java类 如何 递归 遍历自己呢
发布于 1835天前 作者 Hamming 1524 次浏览 复制 上一个帖子 下一个帖子
标签:

高德地图的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层 如何递归 所有数据 保存到 数据库呢

3 回复

有种东西叫递归

@Prev(els = {@EL("uuid()")}) 因为这个 导致 插入 设置的id 变了
有没有办法 判断 id 为空 再赋值

    public static List<Area> areaList = new ArrayList<>();
  public static void getAreaList(List<Districts> list,String pid){
        list.forEach(districts -> {
            Area area =new Area();
            area.setId(R.UU32().toLowerCase());
            area.setParentId(pid);
            area.setAdcode(districts.getAdcode());
            area.setName(districts.getName());
            area.setLevel(districts.getLevel());
            areaList.add(area);
            if(districts.getDistricts()!=null && districts.getDistricts().size()>0){
                getAreaList(districts.getDistricts(),area.getId());
            }
        });
    }

    @At
    @Ok("json")
    public String  initData(){
        //读取文件
        String fileName = "/Users/apple/Desktop/area.txt";
        //读取文件
        BufferedReader br = null;
        StringBuffer sb = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName),"UTF-8")); //这里可以控制编码
            sb = new StringBuffer();
            String line = null;
            while((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        String data = new String(sb); //StringBuffer ==> String
        Amap amap = JSON.parseObject(data,Amap.class);
        if(amap!=null && amap.getDistricts()!=null && amap.getDistricts().size()>0){
            getAreaList(amap.getDistricts(),"0");
        }
        if(areaList!=null && areaList.size()>0){
            areaList.forEach(area -> {
                areaService.insert(area);
            });
        }
        return "successs";

    }

@EL("$me.genId()")

然后写个genId方法,pojo类上

添加回复
请先登陆
回到顶部