使用fastjson解析json文件

在pom中添加

       
			com.alibaba
			fastjson
			1.2.47
		
		
		
			commons-io
			commons-io
			2.6
		

引入fastjson和commons-io工具

	public static void main(String[] args) throws Exception {
        InputStream inputStream = new FileInputStream("test.json");
        String text = IOUtils.toString(inputStream,"utf8");
        List 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"	
	
	}

]

 

你可能感兴趣的:(Java编程)