java对象和json的相互转换--(1) 使用@JsonSerialize对javabean进行json格式化
https://blog.csdn.net/wslyk606/article/details/80301782
以json格式输出数据是常用的方式,这次记录下项目中使用的json格式化方法,
使用的jar包为com.fasterxml.jackson.core:jackson-databind,对应的maven依赖为
<dependency>
<groupId>com.fasterxml.jackson.core</groupId></dependency>
1 首先定义一个Student类
public class Student { private String name; private int age; private int number; public String getName() { return name; } public int getAge() { return age; } public int getNumber() { return number; } public Student(String name, int age, int number) { this.name = name; this.age = age; this.number = number; }}
import com.fasterxml.jackson.databind.annotation.JsonSerialize; import java.util.List; import static com.google.common.collect.Lists.newArrayList; @JsonSerialize(using = StudentsReportSerializer.class)public class StudentsReport { List<String> names = newArrayList(); List<Student> studentList = newArrayList(); public List<String> getNames() { return names; } public List<Student> getStudentList() { return studentList; } public StudentsReport(List<String> names, List<Student> studentList) { this.names = names; this.studentList = studentList; }}
import com.fasterxml.jackson.core.JsonGenerator;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.JsonSerializer;import com.fasterxml.jackson.databind.SerializerProvider; import java.io.IOException; public class StudentsReportSerializer extends JsonSerializer <StudentsReport>{@Override public void serialize(StudentsReport studentsReport, JsonGenerator jgen, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { jgen.writeStartObject(); //start write :"{" jgen.writeFieldName("studentsName"); jgen.writeStartArray();// write "[" for (String name:studentsReport.getNames() ) { jgen.writeObject(name); } jgen.writeEndArray(); jgen.writeFieldName("studentsData"); jgen.writeStartArray(); for (Student student : studentsReport.getStudentList()){ jgen.writeStartObject(); jgen.writeStringField("name",student.getName()); jgen.writeNumberField("age",16); jgen.writeNumberField("number",1); jgen.writeEndObject(); } jgen.writeEndArray(); jgen.writeEndObject(); //end :"}" } }4 进行测试
import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper; import java.util.List; import static com.google.common.collect.Lists.newArrayList; public class TestJson { public static void main(String[] args) throws JsonProcessingException { Student student1 = new Student("Tom",16,1); Student student2 = new Student("Jerry",17,2); List<Student> studentList = newArrayList(); studentList.add(student1); studentList.add(student2); List<String> studentsName = newArrayList(student1.getName(),student2.getName()); StudentsReport report = new StudentsReport(studentsName,studentList); ObjectMapper mapper = new ObjectMapper(); String jsonReport = mapper.writeValueAsString(report); System.out.println(jsonReport); }}
即 :{
"studentsName":[
"Tom",
"Jerry"
],
"studentsData":[
{
"name":"Tom",
"age":16,
"number":1
},
{
"name":"Jerry",
"age":16,
"number":1
}
]
}
ps:使用的newArrayList()是使用了com.google.guava这个包,对应的maven依赖为
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>12.0.1</version>
</dependency>