1.注解(部分)
@JsonProperty:把属性的名称序列化成另一个名称 @JsonFormat:日期格式转换(例如:@JsonFormat(timezone=“GTM+8”,pattern=“yyyy-MM-dd HH:mm:ss”)) @JsonIgnoreProperties:忽略类的某个属性(作用于类,例如:@JsonIgnoreProperties({“属性1”,“属性2”})) @JsonIgnore:忽略类的某个属性(作用于属性,直接在属性加注解即可) @JacksonXmlElementWrapper:可用于指定List等集合类,外围标签名 @JacksonXmlProperty:指定包装标签名,或者指定标签内部属性名 @JacksonXmlRootElement:指定生成xml根标签的名字 @JacksonXmlText:指定当前值,没有xml标签包裹 【tips】 在类中的部分属性使用@JsonProperty和@JacksonXmlProperty注解时,生成的XML字符串标签顺序可能会发生改变
2.方法(主要)
XmlMapper类的方法
实体类(任意类型)解析为xml的字符串 String writeValueAsString(Object var1); 把字符串转换为实体类 <T> T readValue(String var1, Class<T> var2); 读取xml字符串为json格式 JsonNode readTree(String var1);
JsonNode类的方法
获取json中的某个节点值 JsonNode get(String var1);二、使用步骤
1.新建实体类
//java项目www.fhadmin.org @Data public class Student { private String studentNo; private String studentName; }
2.新建测试类
【tips】代码中打印语句后的注释为控制台打印的结果
//java项目www.fhadmin.org public class Demo { //创建XmlMapper对象,用于实体与json和xml之间的相互转换 private static final XmlMapper XML_MAPPER = new XmlMapper(); public static void main(String[] args) throws Exception { Student s = new Student(); s.setStudentNo("202101001"); s.setStudentName("张三"); //实体类解析为xml的字符串(理论上可以将任意类型解析为xml字符串) String xmlStr = XML_MAPPER.writeValueAsString(s); System.out.println(xmlStr);//<Student xmlns=""><studentNo>202101001</studentNo><studentName>张三</studentName></Student> //把字符串转换为实体类 Student student = XML_MAPPER.readValue(xmlStr, Student.class); System.out.println(student);//Student(studentNo=202101001, studentName=张三) //读取xml字符串为json格式 JsonNode jsonNode = XML_MAPPER.readTree(xmlStr); System.out.println(jsonNode);//{"studentNo":"202101001","studentName":"张三"} //获取json中的某一个节点值 JsonNode studentNo = jsonNode.get("studentNo"); System.out.println(studentNo);//"202101001" } }
运行结果
1.实体类部分
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; import lombok.Data; import java.util.List; /** * @java项目 www.fhadmin.org * @version V1.0 * @Title: * @Description: * @date 2021/1/10 21:40 */ @Data @JsonIgnoreProperties({"ignore"})//忽略类的某个属性 public class Clazz { //把属性的名称序列化成另一个名称 @JsonProperty("CLAZZNO") private String clazzNo; //指定包装标签名,或者指定标签内部属性名 @JacksonXmlProperty(localName = "CLAZZNAME") private String clazzName; //忽略类的某个属性 @JsonIgnore private String ignore; private Teacher teacher; //指定外围标签名 @JacksonXmlElementWrapper(localName = "studentList") //指定标签名 @JacksonXmlProperty(localName = "student") private List<Student> studentList; } @Data class Teacher { private String teacherName; private String teacherSex; } @Data class Student { private String studentNo; private String studentName; }
2.测试类部分
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.dataformat.xml.XmlMapper; import java.util.ArrayList; import java.util.List; /** * @java项目www.fhadmin.org * @version V1.0 * @Title: * @Description: * @date 2021/1/10 21:43 */ public class Demo { //创建XmlMapper对象,用于实体与json和xml之间的相互转换 private static final XmlMapper XML_MAPPER = new XmlMapper(); public static void main(String[] args) throws Exception{ Clazz c = new Clazz(); c.setClazzNo("201802"); c.setClazzName("三年级2班"); c.setIgnore("这是被忽略的属性"); Teacher t = new Teacher(); t.setTeacherName("宋老师"); t.setTeacherSex("男"); c.setTeacher(t); List<Student> students = new ArrayList<>(); Student s1 = new Student(); s1.setStudentNo("202101001"); s1.setStudentName("张三"); Student s2 = new Student(); s2.setStudentNo("202101002"); s2.setStudentName("李四"); students.add(s1); students.add(s2); c.setStudentList(students); //实体类解析为xml的字符串 String xmlStr = XML_MAPPER.writeValueAsString(c); System.out.println(xmlStr); //把某一类型的东西转换为实体类 Clazz clazz = XML_MAPPER.readValue(xmlStr, Clazz.class); System.out.println(clazz); //取出Clazz类的studentList属性并打印 List<Student> studentList = clazz.getStudentList(); System.out.println(studentList); //读取xml字符串为json格式 JsonNode jsonNode = XML_MAPPER.readTree(xmlStr); System.out.print(jsonNode); } }
运行结果
<Clazz xmlns=""><teacher><teacherName>宋老师</teacherName><teacherSex>男</teacherSex></teacher><CLAZZNO>201802</CLAZZNO><CLAZZNAME>三年级2班</CLAZZNAME><studentList><student><studentNo>202101001</studentNo><studentName>张三</studentName></student><student><studentNo>202101002</studentNo><studentName>李四</studentName></student></studentList></Clazz> Clazz(clazzNo=201802, clazzName=三年级2班, ignore=null, teacher=Teacher(teacherName=宋老师, teacherSex=男), studentList=[Student(studentNo=202101001, studentName=张三), Student(studentNo=202101002, studentName=李四)]) [Student(studentNo=202101001, studentName=张三), Student(studentNo=202101002, studentName=李四)] {"teacher":{"teacherName":"宋老师","teacherSex":"男"},"CLAZZNO":"201802","CLAZZNAME":"三年级2班","studentList":{"student":{"studentNo":"202101002","studentName":"李四"}}}