现在我们手上有一份 JSON 格式的数据,属性类型均为字符串,且无嵌套
{
"attr1": "value1",
"attr2": "value2",
"attr3": "value3",
"attr4": "value4",
"attr5": "value5"
}
现笔者希望用 Python 实现一份脚本,根据这份数据生成对应的 Java 类代码(包括基本的构造函数、getter 函数、setter 函数、重写 toString 函数、采用驼峰命名法)以及数据库建表的 SQL 语句,效果如下
对应的 Java 类代码
public class Class {
private String attr1;
private String attr2;
private String attr3;
private String attr4;
private String attr5;
public Class(
String attr1,
String attr2,
String attr3,
String attr4,
String attr5
) {
this.attr1=attr1;
this.attr2=attr2;
this.attr3=attr3;
this.attr4=attr4;
this.attr5=attr5;
}
public Class(Class object) {
this.attr1=object.getAttr1();
this.attr2=object.getAttr2();
this.attr3=object.getAttr3();
this.attr4=object.getAttr4();
this.attr5=object.getAttr5();
}
public String getAttr1() {
return attr1;
}
public String getAttr2() {
return attr2;
}
public String getAttr3() {
return attr3;
}
public String getAttr4() {
return attr4;
}
public String getAttr5() {
return attr5;
}
public void setAttr1(String attr1) {
this.attr1=attr1;
}
public void setAttr2(String attr2) {
this.attr2=attr2;
}
public void setAttr3(String attr3) {
this.attr3=attr3;
}
public void setAttr4(String attr4) {
this.attr4=attr4;
}
public void setAttr5(String attr5) {
this.attr5=attr5;
}
@Override
public String toString() {
return
"Class["
+ "attr1=" + attr1 +", "
+ "attr2=" + attr2 +", "
+ "attr3=" + attr3 +", "
+ "attr4=" + attr4 +", "
+ "attr5=" + attr5
+ "]";
}
}
对应的数据库建表的 SQL 语句
CREATE TABLE `table` (
`attr1` varchar(255) DEFAULT NULL,
`attr2` varchar(255) DEFAULT NULL,
`attr3` varchar(255) DEFAULT NULL,
`attr4` varchar(255) DEFAULT NULL,
`attr5` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
用 Python 实现上述效果的代码如下
- 处理含有一份 JSON 数据的文件名
input_name
,默认为input.txt
- 生成含有 Java 类代码的文件名
output_name_1
,默认为output1.txt
- 生成含有数据库建表的 SQL 语句的文件名
output_name_2
,默认为output2.txt
- Java 类名
class_name
- Java 类生成的对象名
object_name
- 数据库表名
table_name
import re
input_name='input.txt'
output_name_1='output1.txt'
output_name_2='output2.txt'
object_name='object'
class_name='Class'
table_name='table'
json_attr_list = []
text1 = 'public class ' + class_name +' {\n' # 类属性声明
text2 = ' public ' + class_name +'(\n' # 类构造函数1参数
text3 = '' # 类构造函数1内容
text4 = ' public ' + class_name + '(' + class_name + ' ' + object_name + ') {\n' # 类构造函数2
text5 = '' # 类的 setter 函数
text6 = '' # 类的 getter 函数
text7 = ' @Override\n public String toString() {\n return\n "' + class_name + '["\n' # 重写累的 toString 函数
text8 = 'CREATE TABLE ' + table_name + ' (\n' # 数据库建表的 SQL 语句
content=open(input_name,'r',encoding='utf-8')
for line in content:
pattern = re.compile('[\{\}"]|:.*')
json_attr = pattern.sub('',line).strip()
if(json_attr == ''): continue
json_attr_list.append(json_attr) # 将 JSON 属性名依次加进列表
content.close()# 关闭文件
json_attr_num = len(json_attr_list)
for index, json_attr in enumerate(json_attr_list):
if(index == json_attr_num-1): is_the_last = True
else: is_the_last = False
text1 += ' private String ' + json_attr + ';\n'
text2 += ' String ' + json_attr
text3 += ' this.' + json_attr + '=' + json_attr +';\n'
text4 += ' this.' + json_attr + '=' + object_name + '.get' + json_attr.capitalize() + '();\n'
text5 += ' public String get' + json_attr.capitalize() + '() {\n return ' + json_attr + ';\n }\n'
text6 += ' public void set' + json_attr.capitalize() + '(String ' + json_attr + ') {\n this.' + json_attr + '=' + json_attr + ';\n }\n'
text7 += ' + "' + json_attr + '=" + ' + json_attr
text8 += '' + json_attr + ' varchar(255) DEFAULT NULL'
if(is_the_last):
text2 += '\n ) {\n'
text3 += ' }\n'
text4 += ' }\n'
text7 += '\n + "]";\n }\n}'
text8 += '\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;'
else:
text2 += ',\n'
text7 += ' +", "\n'
text8 += ',\n'
for index, json_attr in enumerate(json_attr_list):
if(index == json_attr_num-1): is_the_last = True
else: is_the_last = False
with open(output_name_1,"w",encoding='utf-8') as f:
f.write(text1)
f.write(text2)
f.write(text3)
f.write(text4)
f.write(text5)
f.write(text6)
f.write(text7)
f.flush() # 写入硬盘
f.close() # 关闭文件
with open(output_name_2,"w",encoding='utf-8') as f:
f.write(text8)
f.flush() # 写入硬盘
f.close() # 关闭文件