目录
一、Java对象转为JSON字符串
二、JSON字符串转为Java对象
JSON解析器,常见的解析器:Jsonlib,Gson,fastjson,jackson
介绍:Jackson
1、引入需要的Jackson所需要的jar包(三个)
2、创建一个Person类(代码见后面)
3、创建Jackson核心对象,ObjectMapper,用来转换
方法1,writeValue(参数1,参数2)
参数1介绍 |
|
参数2介绍 |
就是需要转换成JSON字符串的对象 |
方法2,writeValueAsString(obj) obj就是需要转换成JSON字符串的对象
4、实例测试
Person.java
package com.json.pojo;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
/**
* 一个人的对象类
* @author xuexue
*
*/
public class Person {
//姓名
private String name;
//年龄
private int age;
//地址
private String address;
//日期
@JsonFormat(pattern = "yyyy/MM/dd hh:mm:ss")
private Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", address=" + address + ", date=" + date + "]";
}
}
Testt.java测试类(输出结果在注释上)
package com.json.test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.json.pojo.Person;
/**
* Person对象转为JSON字符串
* @author xuexue
*
*/
public class Testt {
@Test
public void test() throws JsonProcessingException {
//1、创建Person对象
Person person = new Person();
person.setName("张三");
person.setAge(20);
person.setAddress("河源市");
person.setDate(new Date());
//2、创建Jackson核心对象,ObjectMapper
ObjectMapper mapper = new ObjectMapper();
//3、person对象转为JSON字符串
String valueAsString = mapper.writeValueAsString(person);
System.out.println(valueAsString);//{"name":"张三","age":20,"address":"河源市","date":"2019/09/05 10:36:27"}
}
@Test
public void test2() throws JsonProcessingException {
//1、创建Map集合
HashMap map = new HashMap<>();
map.put("name", "lisi");
map.put("age", 33);
map.put("address", "河源");
//2、创建Jackson核心对象,ObjectMapper
ObjectMapper mapper = new ObjectMapper();
//3、person对象转为JSON字符串
String valueAsString = mapper.writeValueAsString(map);
System.out.println(valueAsString);//{"address":"河源","name":"lisi","age":33}
}
@Test
public void test3() throws JsonProcessingException {
//1、创建Map集合
List list = new ArrayList();
list.add(new Person());
list.add(new Person());
list.add(new Person());
//2、创建Jackson核心对象,ObjectMapper
ObjectMapper mapper = new ObjectMapper();
//3、person对象转为JSON字符串
String valueAsString = mapper.writeValueAsString(list);
System.out.println(valueAsString);//[{"name":null,"age":0,"address":null,"date":null},{"name":null,"age":0,"address":null,"date":null},{"name":null,"age":0,"address":null,"date":null}]
}
}
1、创建JSON字符串
2、创建Jackson核心对象
3、调用readValue方法进行转换,参数1是需要转换的json字符串,参数2是转换后的对象的字节码对象
@Test
public void test4() throws IOException {
//1、创建JSON串
String json = "{\"address\":\"河源\",\"name\":\"lisi\",\"age\":33}";
//2、创建Jackson核心对象,ObjectMapper
ObjectMapper mapper = new ObjectMapper();
//3、JSON字符串转为Person对象
Person person = mapper.readValue(json, Person.class);
System.out.println(person);//[{"name":null,"age":0,"address":null,"date":null},{"name":null,"age":0,"address":null,"date":null},{"name":null,"age":0,"address":null,"date":null}]
}