Java下常见的Json类库有Gson、JSON-lib和Jackson等,Jackson相对来说比较高效,在项目中主要使用Jackson进行JSON和Java对象转换,下面给出一些Jackson的JSON操作方法。
一、准备工作
首先去官网下载Jackson工具包,下载地址http://wiki.fasterxml.com/JacksonDownload。Jackson有1.x系列和2.x系列,截止目前2.x系列的最新版本是2.2.3,2.x系列有3个jar包需要下载:
jackson-core-2.2.3.jar(核心jar包)
jackson-annotations-2.2.3.jar(该包提供Json注解支持)
jackson-databind-2.2.3.jar
具体的项目结构如下图
User类代码如下:
package com.pcmall; import java.text.SimpleDateFormat; import java.util.Date; public class User { private String name; private Integer age; private Date birthday; private String email; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String toString(){ SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd"); String strBirthday = dateformat.format(birthday); return "name=" + name + ";age=" + age + ";birthday=" + strBirthday + ";email=" + email; } }二、JAVA对象转JSON[JSON序列化]
package com.pcmall; import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonSerializeDemo { public static void main(String[] args) throws ParseException, IOException { User user = new User(); user.setName("小民"); user.setEmail("[email protected]"); user.setAge(20); SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd"); user.setBirthday(dateformat.parse("1996-10-01")); /** * ObjectMapper是JSON操作的核心,Jackson的所有JSON操作都是在ObjectMapper中实现。 * ObjectMapper有多个JSON序列化的方法,可以把JSON字符串保存File、OutputStream等不同的介质中。 * writeValue(File arg0, Object arg1)把arg1转成json序列,并保存到arg0文件中。 * writeValue(OutputStream arg0, Object arg1)把arg1转成json序列,并保存到arg0输出流中。 * writeValueAsBytes(Object arg0)把arg0转成json序列,并把结果输出成字节数组。 * writeValueAsString(Object arg0)把arg0转成json序列,并把结果输出成字符串。 */ ObjectMapper mapper = new ObjectMapper(); //User类转JSON //输出结果:{"name":"小民","age":20,"birthday":844099200000,"email":"[email protected]"} String json = mapper.writeValueAsString(user); System.out.println(json); //Java集合转JSON //输出结果:[{"name":"小民","age":20,"birthday":844099200000,"email":"[email protected]"}] List<User> users = new ArrayList<User>(); users.add(user); String jsonlist = mapper.writeValueAsString(users); System.out.println(jsonlist); } }运行后结果如下:
{"name":"小民","age":20,"birthday":844099200000,"email":"[email protected]"} [{"name":"小民","age":20,"birthday":844099200000,"email":"[email protected]"}]三、JSON转Java类[JSON反序列化]
JacksonDeserializeDemo类代码如下:
package com.pcmall; import java.io.IOException; import java.text.ParseException; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonDeserializeDemo { public static void main(String[] args) throws ParseException, IOException { String json = "{\"name\":\"小民\",\"age\":20,\"birthday\":844099200000,\"email\":\"[email protected]\"}"; /** * ObjectMapper支持从byte[]、File、InputStream、字符串等数据的JSON反序列化。 */ ObjectMapper mapper = new ObjectMapper(); User user = mapper.readValue(json, User.class); System.out.println(user); } }运行后结果如下:
name=小民;age=20;birthday=1996-10-01;[email protected]