java对象转json
JSON解析器:
常见的解析器:Jsonlib,Gson,fastjson,jackson
使用步骤:
1.导入jackson相关jar包
2.创建jackson核心对象 ObjectMapper
3.调用ObjectMapper的相关方法转换
转换方法:
writeValue(参数1,obj):
参数1:
File:将obj对象转换为JSON字符串,并保存到指定的文件中
Writer:将obj对象转换为JSON字符串,并将json数据填充到字符输出流中
OutputStream:将obj对象转换为JSON字符串,并将json数据填充到字节输出流中
writeValueAsString(obj):将对象转为json字符串
Person实体类:
package com.example.domain;
public class Person {
//代码效果参考:http://hnjlyzjd.com/xl/wz_24089.html
private String name;private int age;
private String gender;
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
'}';
}
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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
测试类:java转换位json字符串
package com.example.test;
import com.example.domain.Person;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import java.io.File;
import java.io.FileWriter;
public class JacksonTest {
//Java对象转为JSON字符串
@Test
public void test1() throws Exception {
//1.创建Person对象
Person person = new Person();
person.setName("张三");
person.setAge(23);
person.setGender("男");
//2.创建Jackson的核心对象 ObjectMapper
ObjectMapper mapper = new ObjectMapper();
//3.转换
/
转换方法
writeValue(参数1,obj):
参数1:
File:将obj对象转换为JSON字符串,并保存到指定的文件中
Writer:将obj对象转换为JSON字符串,并将json数据填充到字符输出流中
OutputStream:将obj对象转换为JSON字符串,并将json数据填充到字节输出流中
writeValueAsString(obj):将对象转为json字符串
/
String json = mapper.writeValueAsString(person);
//System.out.println(json);// {"name":"张三","age":23,"gender":"男"}
//writeValue:将数据写到d:a.txt文件中
//mapper.writeValue(new File("d:a.txt"), person);
//writeValue:将数据关联到Writer中
mapper.writeValue(new FileWriter(""), person);
}
}
java对象转json-注解
注解:
1.@JsonIgnore:排除属性。
public class Person {
private String name;
private int age;
private String gender;
//@JsonIgnore // 忽略该属性
private Date birthday;
测试:
@Test
public void test2() throws Exception {
//1.创建Person对象
Person person = new Person();
person.setName("张三");
person.setAge(20);
person.setGender("男");
person.setBirthday(new Date());
//2.转换
ObjectMapper mapper = new ObjectMapper();
String string = mapper.writeValueAsString(person);
System.out.println(string);// {"name":"张三","age":23,"gender":"男","birthday":1675910553917}
}
2.@JsonFormat:属性值格式化
public class Person {
private String name;
private int age;
private String gender;
@JsonFormat(pattern = "yyyy-MM-dd") // 格式化该属性
private Date birthday;
测试:
@Test
public void test2() throws Exception {
//1.创建Person对象
Person person = new Person();
person.setName("张三");
person.setAge(20);
person.setGender("男");
person.setBirthday(new Date());
//2.转换
ObjectMapper mapper = new ObjectMapper();
String string = mapper.writeValueAsString(person);
System.out.println(string);// {"name":"张三","age":23,"gender":"男","birthday":1675910553917}
}
java对象转json-List&Map
List集合:数组(对应的是字符串就是字符串对象)
@Test
public void test3() throws Exception {
//1.创建Person对象
Person person = new Person();
person.setName("张三");
person.setAge(23);
person.setGender("男");
person.setBirthday(new Date());
Person person1 = new Person();
person1.setName("张三");
person1.setAge(23);
person1.setGender("男");
person1.setBirthday(new Date());
Person person2 = new Person();
person2.setName("张三");
person2.setAge(23);
person2.setGender("男");
person2.setBirthday(new Date());
//创建list集合
List
people = new ArrayList();
people.add(person);
people.add(person1);
people.add(person2);
//2.转换
ObjectMapper mapper = new ObjectMapper();
String string = mapper.writeValueAsString(people);
//【{"name":"张三","age":23,"gender":"男","birthday":"2023-02-09"},{"name":"张三","age":23,"gender":"男","birthday":"2023-02-09"},{"name":"张三","age":23,"gender":"男","birthday":"2023-02-09"}】
System.out.println(string);
}
Map集合:对象格式一致
@Test
public void test4() throws Exception {
//1.创建map对象
HashMap map = new HashMap();
map.put("name", "张三");
map.put("age", "20");
map.put("gender", "男");
//2.转换
ObjectMapper mapper = new ObjectMapper();
String string = mapper.writeValueAsString(map);
//{"gender":"男","name":"张三","age":"20"}
System.out.println(string);
}