将数据库树结构生成json串

private static void findAllModels(StringBuffer sb, String systemName,
   SysModel model) {
  sb.append("{\"id\":\"");
  sb.append(model.getId());
  sb.append("\"");
  sb.append(",\"displayName\":\"");
  sb.append(model.getDisplayName());
  sb.append("\"");
  sb.append(",\"parentid\":\"");
  sb.append(model.getParentId());
  sb.append("\"");
  System.out.println(model.isMenu());
  if (!model.isMenu()) {
   sb.append(",\"children\":[");
   List<SysModel> sublist = sysModelService
     .querySysModelListByParentId(systemName,
       model.getSysModuleId());
   if(sublist.size()>0){
    for (SysModel sys : sublist) {
     findAllModels(sb, systemName, sys);
    }
    sb = sb.deleteCharAt(sb.length() - 1);
   }
   sb.append("]");
  }
  sb.append("},");
 }
 public static void main(String[] args) throws JSONException {
  StringBuffer sb = new StringBuffer("[");
  List<SysModel> model = sysModelService.querySysModelListByParentId("Authority", "40286d813deefcff013deefd051c0000");
  for(SysModel sm : model){
   findAllModels(sb,"Authority",sm);
  }
  
  sb.deleteCharAt(sb.length() - 1);
  sb.append("]");
  System.out.println(sb.toString());
 }

你可能感兴趣的:(json)