使用fastjson解析json文件

在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"	
	
	}

]

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值