三层json

来源:互联网 发布:淘宝人工 编辑:程序博客网 时间:2024/05/16 16:10
@Test
public  void  test1(){

//省
AreaList  alProvince1=new  AreaList();

alProvince1.setId("1");
   alProvince1.setName("湖北省");
   alProvince1.setPid("0");
   alProvince1.setList(new ArrayList<>());
List<AreaList> listProvince=new   ArrayList<>();

listProvince.add(alProvince1);

List<AreaList>  listCity=new ArrayList<>();
AreaList  alCity1=new  AreaList();
alCity1.setId("11");
alCity1.setName("武汉市");
alCity1.setPid("1");
alCity1.setList(new ArrayList<>());

AreaList  alCity2=new  AreaList();
alCity2.setId("12");
alCity2.setName("荆州市");
alCity2.setPid("1");
alCity2.setList(new ArrayList<>());

listCity.add(alCity1);
listCity.add(alCity2);

List<Area>  listCounty=new  ArrayList<>();
Area   county1=new  Area();
county1.setId("111");
county1.setName("江岸区");
county1.setPid("11");
Area  county2  =new  Area();
county2.setId("121");
county2.setName("金州区");
county2.setPid("12");
Area  county3  =new  Area();
county3.setId("122");
county3.setName("荆州区");
county3.setPid("12");
listCounty.add(county1);
listCounty.add(county2);
listCounty.add(county3);
//区放入市
for(int m=0;m<listCity.size();m++){
for(int n=0;n<listCounty.size();n++){
if(listCity.get(m).getId().equals(listCounty.get(n).getPid())){
listCity.get(m).getList().add(listCounty.get(n));
}
}
}


//市放入省
for(int  i=0; i<listProvince.size();i++){
for(int j=0;j<listCity.size();j++){
if(listProvince.get(i).getId().equals(listCity.get(j).getPid())){
listProvince.get(i).getList().add(listCity.get(j));
}

}

}

System.out.println(JSONArray.fromObject(listProvince).toString());
}
原创粉丝点击