java 解析json生成实体类
package com.myspringbootdemo.demo.todo;
import com.alibaba.fastjson.JSONObject;
import java.io.*;
import java.util.*;
/**
* 读取JSON 生成实体类 一级一级生成
* 2022/10/12 09:19 只生成最外围的
* 2022/10/12 15:46 逐级生成
*/
public class ReadJSONGenerateEntityClass {
private static final String JSONObjectString = "{\"date\":\"2022-10-10 22:23:30\",\"errorCode \":\"0 \",\"message \":\"操作成功 \",\"httpStatus \":200,\"obj\":{\"orderNumber \":\"393374805906554880 \",\"mailNumber \":\"SF1020127608391 \",\"resStatus \":2,\"dateClass\":{\"key1 \":\"value1 \",\"key2 \":\"value2 \",\"key3 \":{\"key11\":\"value11\",\"key22\":\"value22\",\"key33\":\"value33\"}},\"dateArray\":[{\"k11\":\"v11\",\"k12\":\"v12\"},{\"k12\":\"v12\",\"k22\":\"v22\"}]},\"oneDateArray\":[{\"onek11\":\"onev11\",\"onek12\":\"onev12\"},{\"onek12\":\"onev12\",\"onek22\":\"onev22\"}]}";
public final static String PACKAGE_NAME = "【包名】";
public final static String CLASS_NAME = "【类名】";
public final static String IMPORT = "import";
public final static String STRING_REMARKS = "【字段描述】";
public final static String FIELD_NAME = "【类型】 【小写字段名】";
public final static String FIELD_TYPE = "【类型】";
public final static String GETTING = "get【大写字段名】";
public final static String BIG = "【大写字段名】";
public final static String SETTING = "set【大写字段名】";
public final static String MIN = "【小写字段名】";
public static void main(String[] args) {
List<String> JSONFileContent = readFile(System.getProperty("user.dir"));
String join = String.join("", JSONFileContent);
System.out.println(join);
String className = "TestGenerateEntityClass";
String pathName= "./src/test/java/com/myspringbootdemo/demo/autogenerate/";
String packagePath = "com.myspringbootdemo.demo.autogenerate";
List<Map> fileList = new ArrayList<>();
Map<String,Map> fileMap = new HashMap<>();
// List dateString = organizeData(JSONObjectString, className, packagePath);
// System.out.println(fileList.size());
// establishFile(className,pathName,dateString);
// organize(fileList,JSONObjectString);
// organize(fileMap,JSONObjectString,className);
organize(fileMap,join,className);//
for (String s : fileMap.keySet()) {
String classNameStr = StringTest.upperTable(s.trim());
List<String> dateString = newOrganizeData(fileMap.get(s), classNameStr, packagePath);
establishFile(classNameStr,pathName,dateString);
}
}
/**
* 读取存储Json的文件
*/
/**
* 读取文件
*/
private static List<String> readFile(String path) {
List<String> fileList = new ArrayList<>();
String fileName = path + "/src/main/resources/static/test.txt";
System.out.println(fileName);
//读取文件
FileReader fileReader = null;
String s = null;
try {
fileReader = new FileReader(fileName);
//把文件存到缓冲区
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((s = bufferedReader.readLine()) != null){
fileList.add(s);
}
} catch (IOException e) {
e.printStackTrace();
}
return fileList;
}
/**
* 新的整理数据
*/
public static List<String> newOrganizeData(Map zhiAndType , String className, String packagePath) {
//最终的数据
List<String> dateString = new ArrayList<>();
//实体类字段
List<String> fieldList = new ArrayList<>();
//get set 器
List<String> gettingAndSettingList = new ArrayList<>();
List<String> template = TestGetTableStructure.getTemplate();
String containsPackageNameStr = TestGetTableStructure.getStringByString(template, PACKAGE_NAME);
String packageNameStr = containsPackageNameStr.replaceAll(PACKAGE_NAME, packagePath);
dateString.add(packageNameStr);
dateString.add("import java.util.Date;");
dateString.add("import java.io.Serializable;");
dateString.add("import java.util.List;");
String containsClassNameStr = getStringByString(template, CLASS_NAME);
String classNameStr = containsClassNameStr.replaceAll(CLASS_NAME, className);
dateString.add(classNameStr);
for (Object key : zhiAndType.keySet()) {
// 类型
Object value = zhiAndType.get(key);
//首字母大写
String s = StringTest.upperTable(String.valueOf(key));
String containsFieldNameStr = getStringByString(template, FIELD_NAME);
String fieldNameStr = containsFieldNameStr.replaceAll(FIELD_NAME, value + " " + key);
if (String.valueOf(value).equals("HashMap")){
fieldNameStr = containsFieldNameStr.replaceAll(FIELD_NAME, s + " " + key);
}
if(String.valueOf(value).equals("ArrayList")){
fieldNameStr = containsFieldNameStr.replaceAll(FIELD_NAME, "List<" + s + "> " + key);
}
fieldList.add(fieldNameStr);
// get构造器
// public 【类型】 get【大写字段名】() { return 【小写字段名】; }
generateGettingAndSetting(gettingAndSettingList, template, key, value, s, GETTING);
// set构造器
// public void set【大写字段名】(【类型】 【小写字段名】) { this.【小写字段名】 = 【小写字段名】; }
generateGettingAndSetting(gettingAndSettingList, template, key, value, s, SETTING);
}
dateString.addAll(fieldList);
dateString.addAll(gettingAndSettingList);
dateString.add("}");
return dateString;
}
private static void organize(List<Map> fileList,String jsonString) {
Map map = TestJsonUtils.json2Map(jsonString);
Map zhiAndType = new HashMap();
for(Object key : map.keySet()){
Object valeue = map.get(key);
String type = TestAnalysisJSON.getTypeByObject(valeue);
zhiAndType.put(key,type);
if (type.equals("HashMap")){
organize(fileList,new JSONObject((Map)valeue).toJSONString());
}
if (type.equals("ArrayList")){
List<Map> list = (List) valeue;
JSONObject jsonObject = new JSONObject(list.get(0));
organize(fileList, jsonObject.toJSONString());
}
}
fileList.add(zhiAndType);
}
public static void organize(Map<String,Map> fileMap,String jsonString,String name) {
Map map = TestJsonUtils.json2Map(jsonString);
Map zhiAndType = new HashMap();
for(Object key : map.keySet()){
Object valeue = map.get(key);
String type = TestAnalysisJSON.getTypeByObject(valeue);
zhiAndType.put(key,type);
if (type.equals("HashMap")){
organize(fileMap,new JSONObject((Map)valeue).toJSONString(),String.valueOf(key));
}
if (type.equals("ArrayList")){
List<Map> list = (List) valeue;
JSONObject jsonObject = new JSONObject(list.get(0));
organize(fileMap, jsonObject.toJSONString(),String.valueOf(key));
}
}
fileMap.put(name,zhiAndType);
}
/**
* 整理数据
* @param className 类名
* @param packagePath 包路径
*/
private static List<String> organizeData (String jsonObjectString, String className, String packagePath) {
//最终的数据
List<String> dateString = new ArrayList<>();
//实体类字段
List<String> fieldList = new ArrayList<>();
//get set 器
List<String> gettingAndSettingList = new ArrayList<>();
List<String> template = TestGetTableStructure.getTemplate();
Map map = TestJsonUtils.json2Map(jsonObjectString);
// System.out.println(map);
Set keySet = map.keySet();
Map zhiAndType = new HashMap();
for (Object key : keySet) {
zhiAndType.put(key,TestAnalysisJSON.getTypeByObject(map.get(key)));
}
// System.out.println(zhiAndType);
String containsPackageNameStr = TestGetTableStructure.getStringByString(template, PACKAGE_NAME);
String packageNameStr = containsPackageNameStr.replaceAll(PACKAGE_NAME, packagePath);
dateString.add(packageNameStr);
dateString.add("import java.util.Date;");
dateString.add("import java.io.Serializable;");
dateString.add("import java.util.List;");
String containsClassNameStr = getStringByString(template, CLASS_NAME);
String classNameStr = containsClassNameStr.replaceAll(CLASS_NAME, className);
dateString.add(classNameStr);
for (Object key : zhiAndType.keySet()) {
// 类型
Object value = zhiAndType.get(key);
//首字母大写
String s = StringTest.upperTable(String.valueOf(key));
String containsFieldNameStr = getStringByString(template, FIELD_NAME);
String fieldNameStr = containsFieldNameStr.replaceAll(FIELD_NAME, value + " " + key);
if (String.valueOf(value).equals("HashMap")){
fieldNameStr = containsFieldNameStr.replaceAll(FIELD_NAME, s + " " + key);
}
if(String.valueOf(value).equals("ArrayList")){
fieldNameStr = containsFieldNameStr.replaceAll(FIELD_NAME, "List<" + s + "> " + key);
}
fieldList.add(fieldNameStr);
// get构造器
// public 【类型】 get【大写字段名】() { return 【小写字段名】; }
generateGettingAndSetting(gettingAndSettingList, template, key, value, s, GETTING);
// set构造器
// public void set【大写字段名】(【类型】 【小写字段名】) { this.【小写字段名】 = 【小写字段名】; }
generateGettingAndSetting(gettingAndSettingList, template, key, value, s, SETTING);
}
dateString.addAll(fieldList);
dateString.addAll(gettingAndSettingList);
dateString.add("}");
return dateString;
}
/**
* 生成get 和 set 器
* @param gettingAndSettingList get和set的存储List
* @param template 模板
* @param key 字段名
* @param value 字段类型
* @param s 字段类型首字母大写
* @param setting 模板中的get或set
*/
private static void generateGettingAndSetting(List<String> gettingAndSettingList, List<String> template, Object key, Object value, String s, String setting) {
String str5 = getStringByString(template, setting);
String str6 = str5.replaceAll(FIELD_TYPE, String.valueOf(value));
String str7 = str6.replaceAll(BIG, s);
String str8 = str7.replaceAll(MIN, String.valueOf(key));
if (String.valueOf(value).equals("HashMap")){
str5 = getStringByString(template, setting);
str6 = str5.replaceAll(FIELD_TYPE, s);
str7 = str6.replaceAll(BIG, s);
str8 = str7.replaceAll(MIN, String.valueOf(key));
}
if (String.valueOf(value).equals("ArrayList")){
str5 = getStringByString(template, setting);
str6 = str5.replaceAll(FIELD_TYPE, "List<"+s+">");
str7 = str6.replaceAll(BIG, s);
str8 = str7.replaceAll(MIN, String.valueOf(key));
}
gettingAndSettingList.add(str8);
}
/**
* 生成文件
* @param className 文件名
* @param pathName 文件地址
* @param dateString 文件内容
*/
public static void establishFile(String className, String pathName,List<String> dateString){
//System.out.println(dateString);
try {
TestGetTableStructure.createFileAndPathAndWidth(pathName,className+".java",dateString);
}catch (Exception e){
System.out.println("异常了");
}
}
public static File establishFile2(String className, String pathName,List<String> dateString){
File fileAndPathAndWidth2=null;
try {
fileAndPathAndWidth2 = createFileAndPathAndWidth2(pathName, className + ".java", dateString);
}catch (Exception e){
System.out.println("异常了!");
}
return fileAndPathAndWidth2;
}
public static String getStringByString(List<String> template,String type){
String str = "";
for (String s : template) {
if (s.contains(type)){
str = s;
return str;
}
}
return str;
}
public static File createFileAndPathAndWidth2(String directoryPath,String fileName,List<String> dateString) throws Exception{
File file = new File(directoryPath);
if (!file.exists()){
//创建文件夹
if (file.mkdirs()){
System.out.println("创建" + directoryPath + "成功");
} else {
System.out.println("创建" + directoryPath + "失败");
}
}
String filePath = directoryPath + fileName;
System.out.println("filePath::" + filePath);
File file2 = new File(filePath);
if (file2.exists()){
System.out.println("该文件已存在,不能重复创建");
} else {
//创建文件
if (file2.createNewFile()){
System.out.println("文件创建成功");
//写入内容
BufferedWriter writer = new BufferedWriter(new FileWriter(filePath));
for (String s : dateString) {
writer.write(s);
writer.write("\n");
}
writer.close();
} else {
System.out.println("文件创建失败");
}
}
return file2;
}
}