原文: https://howtodoinjava.com/jaxb/convert-object-to-json-moxy/
Java 示例将 Java 对象转换为 JSON 字符串或将 JSON 写入文件。 本示例将 MOXy 与 JAXB 一起使用,以将 Java 对象编组为 JSON。 MOXy 实现 JAXB,使开发人员可以通过注解提供其映射信息,并提供 JAXB 默认不提供的许多丰富功能。
1. MOXy 依赖
包括 MOXy 到项目运行时。
<dependency><groupId>org.eclipse.persistence</groupId><artifactId>org.eclipse.persistence.moxy</artifactId><version>2.5.2</version></dependency>
2. Java 对象到 JSON 字符串
2.1 添加 JAXB 注解
@XmlRootElement(name = "employee")@XmlAccessorType(XmlAccessType.PROPERTY)public class Employee implements Serializable {private static final long serialVersionUID = 1L;private Integer id;private String firstName;private String lastName;private Department department;public Employee() {super();}//Setters and Getters}
@XmlRootElement(name = "department")@XmlAccessorType(XmlAccessType.PROPERTY)public class Department implements Serializable {private static final long serialVersionUID = 1L;Integer id;String name;public Department() {super();}//Setters and Getters}
2.2 添加jaxb.properties
当您获得JAXBContext的实例时,JAXB 将检查jaxb.properties文件并构造上下文。 在这里,您从 MOXy 库中注入了JAXBContextFactory。
将
jaxb.properties文件放在放置 JAXB 注解类的同一包中。
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
2.3 将对象转换为 JSON
现在使用javax.xml.bind.Marshaller类将对象转换为 json。
package com.howtodoinjava.demo;import java.io.StringWriter;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.Marshaller;import org.eclipse.persistence.jaxb.MarshallerProperties;import com.howtodoinjava.demo.model.Department;import com.howtodoinjava.demo.model.Employee;public class JaxbExample{public static void main(String[] args){Employee employee = new Employee(1, "Lokesh", "Gupta", new Department(101, "IT"));jaxbObjectToJSON(employee);}private static void jaxbObjectToJSON(Employee employee){try{JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);Marshaller jaxbMarshaller = jaxbContext.createMarshaller();// To format JSONjaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);//Set JSON typejaxbMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");jaxbMarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);//Print JSON String to ConsoleStringWriter sw = new StringWriter();jaxbMarshaller.marshal(employee, sw);System.out.println(sw.toString());}catch (JAXBException e){e.printStackTrace();}}}
程序输出:
{"employee" : {"department" : {"id" : 101,"name" : "IT"},"firstName" : "Lokesh","id" : 1,"lastName" : "Gupta"}}
阅读更多:将 Java 对象转换为 XML
3. Java 对象到 JSON 文件
使用上面的代码,现在输出到 json 文件。
package com.howtodoinjava.demo;import java.io.File;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.Marshaller;import org.eclipse.persistence.jaxb.MarshallerProperties;import com.howtodoinjava.demo.model.Department;import com.howtodoinjava.demo.model.Employee;public class JaxbExample{public static void main(String[] args){Employee employee = new Employee(1, "Lokesh", "Gupta", new Department(101, "IT"));jaxbObjectToJSON(employee);}private static void jaxbObjectToJSON(Employee employee){try{JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);Marshaller jaxbMarshaller = jaxbContext.createMarshaller();// To format JSONjaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);//Set JSON typejaxbMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");jaxbMarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);//Store JSON to FileFile file = new File("employee.json");jaxbMarshaller.marshal(employee, file);}catch (JAXBException e){e.printStackTrace();}}}
程序输出:
{"employee" : {"department" : {"id" : 101,"name" : "IT"},"firstName" : "Lokesh","id" : 1,"lastName" : "Gupta"}}
请在评论部分将该 json 转换示例相关的问题发送给我。
学习愉快!
