添加依赖:
<!-- jackson依赖 -->
<dependency>
<groupId>com.faster .jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
<!-- 核心包 -->
<dependency>
<groupId>com.faster .jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.5</version>
</dependency>
<!-- 提供注解 -->
<dependency>
<groupId>com.faster .jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
实体类:
/**
* @author : lichenfei
* @date : 2018年12月20日
* @time : 下午3:46:07
*
*/
package com.lcf.entity;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.faster .jackson.annotation.JsonFormat;
/**
* @author : lichenfei
* @date : 2018年12月20日
* @time : 下午3:46:07
*
*/
public class User {
private String name;
private int age;
@JsonFormat(pattern = \"yyyy-MM-dd HH:mm:ss\")
private Date birthday;
private String sex;
public User() {
super();
}
public User(String name, int age, String sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}
public User(String name, int age, Date birthday, String sex) {
super();
this.name = name;
this.age = age;
this.birthday = birthday;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getBirthday() {
if (birthday == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(\"yyyy-MM-dd HH:mm:ss\");
return sdf.format(birthday);
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return \"User [name=\" + name + \", age=\" + age + \", birthday=\" + birthday + \", sex=\" + sex + \"]\";
}
}
测试类:
/**
* @author : lichenfei
* @date : 2018年12月20日
* @time : 下午3:50:04
*
*/
package com.lcf.test;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import org.junit.Test;
import com.faster .jackson.annotation.JsonFormat;
import com.faster .jackson.annotation.JsonInclude.Include;
import com.faster .jackson.core.JsonParseException;
import com.faster .jackson.core.JsonProcessingException;
import com.faster .jackson.databind.DeserializationConfig;
import com.faster .jackson.databind.JsonMappingException;
import com.faster .jackson.databind.JsonNode;
import com.faster .jackson.databind. Mapper;
import com.faster .jackson.databind.node.JsonNodeFactory;
import com.lcf.entity.User;
/**
* @author : lichenfei
* @date : 2018年12月20日
* @time : 下午3:50:04
*
*/
public class Demo1 {
private static final Mapper mapper = new Mapper();
static {
mapper.setTimeZone(TimeZone.getDefault());// getTimeZone(\"GMT+8:00\")
}
/**
* java对象转--->json字符串/json序列化
*
*/
@Test
public void method1() throws JsonProcessingException {
User user = new User(\"小明\", 12, new Date(), \"男\");
String userJson = toJsonString(user);
// 输出结果: {\"name\":\"小明\",\"age\":12,\"birthday\":\"2018-12-20 16:29:13\",\"sex\":\"男\"}
System.out.println(userJson);
// java集合转json字符串
List<User> users = new ArrayList<User>();
User user1 = new User(\"小红\", 13, new Date(), \"nv\");
User user2 = new User(\"小强\", 11, null, \"nv\");
users.add(user1);
users.add(user2);
// 输出结果: [{\"name\":\"小红\",\"age\":13,\"birthday\":\"2018-12-20 17:07:47\",\"sex\":\"nv\"},{\"name\":\"小强\",\"age\":11,\"sex\":\"nv\"}]---->这里生日为null的值没有打印出来
System.out.println(toJsonString(users));
}
/**
* 反序列化
* json对象转换为实体类---->日期类型转换出错,需要在实体类添加注解: @JsonFormat(pattern = \"yyyy-MM-dd HH:mm:ss\")
*/
@Test
public void method2() throws JsonParseException, JsonMappingException, IOException {
String strJson = \"{\\\"name\\\":\\\"小明\\\",\\\"age\\\":12,\\\"birthday\\\":\\\"2018-12-20 16:29:13\\\",\\\"sex\\\":\\\"男\\\"}\";
User user3 = jsonToEntity(strJson, User.class);
// 输出 : User [name=小明, age=12, birthday=Fri Dec 21 00:29:13 CST 2018, sex=男]
System.out.println(user3.toString());
}
/**
* 解析json数据
* @throws IOException
*/
@Test
public void method3() throws IOException {
String strJson = \"{\\\"name\\\":\\\"小明\\\",\\\"age\\\":12,\\\"birthday\\\":\\\"2018-12-20 16:29:13\\\",\\\"sex\\\":\\\"男\\\"}\";
JsonNode jsonNode = mapper.readTree(strJson);// 读取
String name = jsonNode.findValue(\"name\").asText();
int age = jsonNode.findValue(\"age\").asInt();
String sex = jsonNode.findValue(\"sex\").asText();
String bir = jsonNode.findValue(\"birthday\").asText();
// 输出: name:小明,age:12,sex:男,bir:2018-12-20 16:29:13
System.out.println(\"name:\" + name + \",age:\" + age + \",sex:\" + sex + \",bir:\" + bir);
}
/**
* java对象转换为json
*
* @author : lichenfei
* @date : 2018年12月20日
* @time : 下午5:11:38
* @param
* @return
* @throws JsonProcessingException
*/
public static String toJsonString( ) throws JsonProcessingException {
mapper.setSerializationInclusion(Include.NON_NULL);// 当属性为null的时候不参加序列化
return mapper.writeValueAsString( );
}
/**
* json转换为java对象
*
* @author : lichenfei
* @date : 2018年12月20日
* @time : 下午6:17:12
* @param json
* @param T
* @return
* @throws JsonParseException
* @throws JsonMappingException
* @throws IOException
*
*/
public static <T> T jsonToEntity(String json, Class<T> T)
throws JsonParseException, JsonMappingException, IOException {
return mapper.readValue(json, T);
}
/**
*
* 注解: @JsonProperty(\"***\") --->输出时改变属性名称
* @JsonIgnore --->忽略此属性,不会进行输出
* @JsonFormat(pattern = \"yyyy-MM-dd HH:mm:ss\", timezone = \"GMT+8\")指定Date类字段序列化时的格式
* @JsonInclude(Include.NON_EMPTY) 仅在属性不为空时序列化此字段,对于字符串,即null或空字符串
* @JsonIgnoreType 类注解,序列化时忽略此类
*
*/
// 通过该方法对mapper对象进行设置,所有序列化的对象都将按改规则进行系列化
// Include.Include.ALWAYS 默认
// Include.NON_DEFAULT 属性为默认值不序列化
// Include.NON_EMPTY 属性为 空(“”) 或者为 NULL 都不序列化
// Include.NON_NULL 属性为NULL 不序列化
}
新人一枚,有什么错误希望多多指导,刚好学习完分享一下,后边对 的操作会及时补充.
继续阅读与本文标签相同的文章
下一篇 :
应用机器学习:传道解惑指南
-
小学生发现丰巢快递柜刷脸取件bug 用照片就能打开
2026-05-18栏目: 教程
-
WIPO:中国专利申请量占世界一半 去年申请专利数量达154万件
2026-05-18栏目: 教程
-
Yotta主网上线在即,带你玩转YTA挖矿攻略!
2026-05-18栏目: 教程
-
将制造业作为发展数字经济的主战场!
2026-05-18栏目: 教程
-
如何成为一名优秀的初级开发者?
2026-05-18栏目: 教程
