JSON和bean互转

需要用到的类:

net.sf.json.JSONObject  

在包json-lib-2.3-jdk15

maven依赖:

1
<dependency>
	<groupId>net.sf.json-lib</groupId>
	<artifactId>json-lib</artifactId>
	<version>2.4</version>
	<classifier>jdk15</classifier>
</dependency>

将bean转换成JSONObject,调用JSONObject.fromObject(object)
如:

JSONObject jsonObject = JSONObject.fromObject(user);

将JSONObject转换成bean,调用JSONObject.toBean(jsonObject, beanClass)
如:

User user = (User) JSONObject.toBean(jsonObject, User.class);