原文: https://howtodoinjava.com/jaxb/convert-json-to-java-object-moxy/
Java 示例将 JSON 转换为 Java 对象。 您可以将 JSON 字符串解组到对象或将 JSON 文件解组到对象。 此示例将 MOXy 与 JAXB 一起使用,以将 JSON 解组到 Java 对象。 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.将 JSON 文件转换为 Java 对象
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
3.将 JSON 文件转换为对象
现在使用javax.xml.bind.UnMarshaller类将 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 javax.xml.bind.Unmarshaller;import org.eclipse.persistence.jaxb.MarshallerProperties;import org.eclipse.persistence.jaxb.UnmarshallerProperties;import com.howtodoinjava.demo.model.Department;import com.howtodoinjava.demo.model.Employee;public class JaxbExample{public static void main(String[] args){String fileName = "employee.json";jaxbJsonToObject(fileName);}private static void jaxbJsonToObject(String fileName) {File xmlFile = new File(fileName);JAXBContext jaxbContext;try{jaxbContext = JAXBContext.newInstance(Employee.class);Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();//Set JSON typejaxbUnmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");jaxbUnmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, true);Employee employee = (Employee) jaxbUnmarshaller.unmarshal(xmlFile);System.out.println(employee);}catch (JAXBException e){e.printStackTrace();}}}
程序输出:
Employee [id=1, firstName=Lokesh, lastName=Gupta, department=Department [id=101, name=IT]]
读取的 JSON 文件为:
{"employee" : {"department" : {"id" : 101,"name" : "IT"},"firstName" : "Lokesh","id" : 1,"lastName" : "Gupta"}}
4.将 JSON 字符串转换为 Java 对象
您可以获取字符串形式的 JSON,然后直接填充到 Java 对象。
package com.howtodoinjava.demo;import java.io.File;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.Marshaller;import javax.xml.bind.Unmarshaller;import org.eclipse.persistence.jaxb.MarshallerProperties;import org.eclipse.persistence.jaxb.UnmarshallerProperties;import com.howtodoinjava.demo.model.Department;import com.howtodoinjava.demo.model.Employee;public class JaxbExample{public static void main(String[] args){String jsonString = "{\"employee\":{\"department\":{\"id\":101,\"name\":\"IT\"},\"firstName\":\"Lokesh\",\"id\":1,\"lastName\":\"Gupta\"}}";jaxbJsonStringToObject(jsonString);}private static void jaxbJsonStringToObject(String jsonString){JAXBContext jaxbContext;try{jaxbContext = JAXBContext.newInstance(Employee.class);Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();//Set JSON typejaxbUnmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");jaxbUnmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, true);Employee employee = (Employee) jaxbUnmarshaller.unmarshal(new StringReader(jsonString));System.out.println(employee);}catch (JAXBException e){e.printStackTrace();}}}
程序输出:
Employee [id=1, firstName=Lokesh, lastName=Gupta, department=Department [id=101, name=IT]]
5.将 Java 对象转换为 JSON
将 Java 对象转换为 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
在评论部分中,向我发送您与编组和解组 json 有关的问题。
学习愉快!
