在pom中添加
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
引入fastjson和commons-io工具
public static void main(String[] args) throws Exception {
InputStream inputStream = new FileInputStream("test.json");
String text = IOUtils.toString(inputStream,"utf8");
List<Test> s=JSON.parseArray(text, Test.class);
String path = "sql.txt";
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(path,true)));
for (Test d:s) {
//String SQL = "update HDJC_BASE_MONITOR2 set RTSPURL = '"+d.getRtspUrl()+"',NAME = '"+d.getName()+"',UM = '"+d.getUsername()+"',PW = '"+d.getPassword()+"' where ID = '"+d.getId()+"'";
String SQL = "insert into test (ID,NAME,PASSWORD)"
+ "VALUES('"+d.getId()+"','"+d.getName()+"','"+d.getPassword()+"')";
System.out.println(SQL);
out.write(SQL);
out.write("\n");
out.flush();
}
out.close();
}
Test.java
@Data
public class Test {
private String id;
private String name;
private String password;
}
test.json
[
{
"id":1,
"name":"tom",
"password":"123456"
}
]