public class Json2Object { private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private static FileWriter getFileWriter(String className) throws IOException { File targetPath = new File("gen"); if (!targetPath.exists()) { targetPath.mkdirs(); } File file = new File(targetPath, StringUtils.capitalize(className) + ".java"); return new FileWriter(file); } private static void cleanTargetPath() throws IOException { FileUtils.deleteDirectory(new File("gen")); } private static JSON getInputJson() throws IOException { System.out.println("請輸入要轉化爲對象的Json字符串!"); String json = br.readLine(); try { json = StringUtils.strip(json); if (json.startsWith("[")) { return JSONArray.parseArray(json); } else { return JSONObject.parseObject(json, JSONObject.class); } } catch (Exception e) { System.err.println("輸入的不是標準的json,請從新輸入Json字符串!"); return getInputJson(); } } private static String getPkgOrClassName(String promotion) throws IOException { System.out.println("請輸入" + promotion + ""); String json = br.readLine(); if (StringUtils.isNotEmpty(json)) { return json; } else { System.out.println("輸入的" + promotion + "爲空,請從新輸入!"); return getPkgOrClassName(promotion); } } private static void genJavaBean(JSON json, String pkgName, String className) throws IOException { List<String> readLines = ResouceUtil.getResouceLines("ClassTempldate.txt"); readLines = LineProcessor.changeLines(readLines, "###pkgName###", pkgName); readLines = LineProcessor.changeLines(readLines, "###VoName###", StringUtils.capitalize(className)); StringBuilder propertyBuilder = new StringBuilder(); if (JSONObject.class.equals(json.getClass())) { JSONObject jsonObject = (JSONObject) json; for (String key : jsonObject.keySet()) { Object object = jsonObject.get(key); String matchType = matchType(object, key); propertyBuilder.append("public ").append(matchType).append(" ").append(key).append(";\r\n\t"); if (JSONObject.class.equals(object.getClass())) { genJavaBean((JSONObject) object, pkgName, StringUtils.capitalize(key)); } else if (JSONArray.class.equals(object.getClass())) { JSONArray jsonArray = (JSONArray) object; Iterator<Object> iterator = jsonArray.iterator(); if (iterator.hasNext()) {// 若是集合的內容不爲空的話,斷定裏面的內容的類型 Object next = iterator.next(); if (JSONObject.class.equals(next.getClass()) || JSONArray.class.equals(next.getClass())) { String innerClassName = getPkgOrClassName(next.toString()); genJavaBean((JSON) next, pkgName, innerClassName); } } } } } else if (JSONArray.class.equals(json.getClass())) { JSONArray jsonArray = (JSONArray) json; Iterator<Object> iterator = jsonArray.iterator(); if (iterator.hasNext()) {// 若是集合的內容不爲空的話,斷定裏面的內容的類型 Object next = iterator.next(); System.out.println(next.getClass().getName()); String matchType = matchType(next.getClass(), ""); if (JSONObject.class.equals(next.getClass()) || JSONArray.class.equals(next.getClass())) { String innerClassName = getPkgOrClassName(next.toString()); if (StringUtils.isEmpty(matchType)) { propertyBuilder.append("public ").append(StringUtils.capitalize(innerClassName)).append("[] ;\r\n\t"); } genJavaBean((JSON) next, pkgName, innerClassName); } else { propertyBuilder.append("public ").append(matchType).append("[] ;\r\n\t"); } } else { propertyBuilder.append("public String[] ").append(" ;\r\n\t"); } } else { System.err.println("錯誤的類型,請修改程序支持"); } readLines = LineProcessor.changeLines(readLines, "###columns###", propertyBuilder.toString()); FileWriter fileWriter = getFileWriter(className); IOUtils.writeLines(readLines, null, fileWriter); fileWriter.close(); } public static String matchType(Object object, String key) { Class<?> clazz = object.getClass(); if (String.class.equals(clazz)) { return "String"; } else if (Integer.class.equals(clazz)) { return "int"; } else if (Float.class.equals(clazz)) { return "float"; } else if (Date.class.equals(clazz)) { return "Date"; } else if (Boolean.class.equals(clazz)) { return "boolean"; } else if (Long.class.equals(clazz) || BigDecimal.class.equals(clazz)) { return "long"; } else if (JSONObject.class.equals(clazz)) { return StringUtils.capitalize(key); } else if (JSONArray.class.equals(clazz)) { JSONArray jsonArray = (JSONArray) object; Iterator<Object> iterator = jsonArray.iterator(); if (iterator.hasNext()) {// 若是集合的內容不爲空的話,斷定裏面的內容的類型 Object next = iterator.next(); return matchType(next, key) + "[]"; } else {// 若是集合爲空的話,默認爲空便可 return "String[]"; } } throw new RuntimeException("未知類型" + clazz); } public static void main(String[] args) throws IOException { JSON json = getInputJson(); // String pkgName = getPkgOrClassName("包名"); String pkgName = "com.test.cn"; // String className = getPkgOrClassName("類名"); String className = "query"; cleanTargetPath(); System.out.println(json.getClass()); genJavaBean(json, pkgName, className); }} java
public class ResouceUtil { json
public static List<String> getResouceLines(String fileName){ try(InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);) { return IOUtils.readLines(resourceAsStream); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } }public class LineProcessor { public static List<String> changeLines(List<String> readLines,String templdate, String targetValue) { List<String> resultLines = new ArrayList<String>(); for (String line: readLines) { line = line.replace(templdate,targetValue); resultLines.add(line); } return resultLines; } }ClassTempldate.txt中的內容爲: api
package ###pkgName###.domain;
import com.alibaba.fastjson.JSONObject;
public class ###VoName### {
###columns###
@Override
public String toString() {
return JSONObject.toJSONString(this);
}
} app