Java下利用Jackson进行JSON解析和序列化示例
作者:死神的丧钟 时间:2023-09-27 14:37:38
Java下常见的Json类库有Gson、JSON-lib和Jackson等,Jackson相对来说比较高效,在项目中主要使用Jackson进行JSON和Java对象转换,下面给出一些Jackson的JSON操作方法。
一、准备工作
首先去官网下载Jackson工具包。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(下载地址)
//JSON序列化和反序列化使用的User类
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;
}
}
二、JAVA对象转JSON[JSON序列化]
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonDemo {
public static void main(String[] args) throws ParseException, IOException {
User user = new User();
user.setName("小民");
user.setEmail("xiaomin@sina.com");
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":"xiaomin@sina.com"}
String json = mapper.writeValueAsString(user);
System.out.println(json);
//Java集合转JSON
//输出结果:[{"name":"小民","age":20,"birthday":844099200000,"email":"xiaomin@sina.com"}]
List<User> users = new ArrayList<User>();
users.add(user);
String jsonlist = mapper.writeValueAsString(users);
System.out.println(jsonlist);
}
}
三、JSON转Java类[JSON反序列化]
import java.io.IOException;
import java.text.ParseException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonDemo {
public static void main(String[] args) throws ParseException, IOException {
String json = "{\"name\":\"小民\",\"age\":20,\"birthday\":844099200000,\"email\":\"xiaomin@sina.com\"}";
/**
* ObjectMapper支持从byte[]、File、InputStream、字符串等数据的JSON反序列化。
*/
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(json, User.class);
System.out.println(user);
}
}
四、JSON注解
Jackson提供了一系列注解,方便对JSON序列化和反序列化进行控制,下面介绍一些常用的注解。
@JsonIgnore 此注解用于属性上,作用是进行JSON操作时忽略该属性。
@JsonFormat 此注解用于属性上,作用是把Date类型直接转化为想要的格式,如@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss")。
@JsonProperty 此注解用于属性上,作用是把该属性的名称序列化为另外一个名称,如把trueName属性序列化为name,@JsonProperty("name")。
import java.util.Date;
import com.fasterxml.jackson.annotation.*;
public class User {
private String name;
//不JSON序列化年龄属性
@JsonIgnore
private Integer age;
//格式化日期属性
@JsonFormat(pattern = "yyyy年MM月dd日")
private Date birthday;
//序列化email属性为mail
@JsonProperty("mail")
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;
}
}
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonDemo {
public static void main(String[] args) throws ParseException, IOException {
User user = new User();
user.setName("小民");
user.setEmail("xiaomin@sina.com");
user.setAge(20);
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
user.setBirthday(dateformat.parse("1996-10-01"));
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(user);
System.out.println(json);
//输出结果:{"name":"小民","birthday":"1996年09月30日","mail":"xiaomin@sina.com"}
}
}
来源:http://blog.csdn.net/accountwcx/article/details/24585987
标签:jackson,json,java
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Kotlin 基础教程之类、对象、接口
2022-03-12 04:22:15
Android 6.0动态权限申请教程
2023-09-26 16:43:56
Java后台线程操作示例【守护线程】
2023-11-25 01:35:44
Spring框架七大模块简单介绍
2023-03-10 10:25:23
![](https://img.aspxhome.com/file/2023/1/64901_0s.jpg)
Android OpenGL仿自如APP裸眼3D效果详解
2023-12-23 21:37:05
![](https://img.aspxhome.com/file/2023/0/88110_0s.png)
详解springmvc控制登录用户session失效后跳转登录页面
2021-12-08 19:36:40
Hibernate5新特性介绍
2023-01-02 16:28:09
mybaties plus selectMaps和selectList的区别说明
2021-07-15 11:18:29
![](https://img.aspxhome.com/file/2023/5/68225_0s.png)
Spring Boot Security 结合 JWT 实现无状态的分布式API接口
2021-06-04 05:01:17
C#实现在购物车系统中生成不重复订单号的方法
2022-06-21 04:03:09
C语言枚举(enum)和联合(union)实例分享
2023-06-17 01:56:42
android开发之关闭所有的activity的方法
2022-01-16 18:21:29
解决MyEclipse出现the user operation is waiting的问题
2022-05-02 21:44:07
![](https://img.aspxhome.com/file/2023/0/64750_0s.gif)
基于Spring Boot应用ApplicationEvent案例场景
2023-08-17 22:38:12
![](https://img.aspxhome.com/file/2023/0/96120_0s.png)
Android内置SQLite的使用详细介绍
2021-10-24 11:44:17
![](https://img.aspxhome.com/file/2023/2/139372_0s.png)
详解C# 匿名对象(匿名类型)、var、动态类型 dynamic
2022-03-26 18:43:02
![](https://img.aspxhome.com/file/2023/8/72458_0s.png)
Android 媒体库数据更新方法总结
2022-04-24 10:22:17
详解SpringBoot中的tomcat优化和修改
2022-12-31 04:00:44
Android屏幕旋转 处理Activity与AsyncTask的最佳解决方案
2023-03-22 07:02:50
![](https://img.aspxhome.com/file/2023/6/91696_0s.gif)
java定时任务Timer和TimerTask使用详解
2023-07-13 00:29:33