🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 使用 Moxy 和 Jaxb 将 JSON 转换为 Java 对象的示例 > 原文: [https://howtodoinjava.com/jaxb/convert-json-to-java-object-moxy/](https://howtodoinjava.com/jaxb/convert-json-to-java-object-moxy/) Java 示例**将 JSON 转换为 Java 对象**。 您可以将 **JSON 字符串解组到对象**或将 **JSON 文件解组到对象**。 此示例将 [MOXy](https://wiki.eclipse.org/EclipseLink/Examples/MOXy) 与 JAXB 一起使用,以将 JSON 解组到 Java 对象。 MOXy 实现 JAXB,使开发人员可以通过注解提供其映射信息,并提供 JAXB 默认不提供的许多丰富功能。 ## 1\. MOXy 依赖 包括 MOXy 到项目运行时。 ```java <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>org.eclipse.persistence.moxy</artifactId> <version>2.5.2</version> </dependency> ``` ## 2.将 JSON 文件转换为 Java 对象 #### 2.1 添加 JAXB 注解 ```java @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 } ``` ```java @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 注解类的同一包中。 ```java javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory ``` #### 3.将 JSON 文件转换为对象 现在使用`javax.xml.bind.UnMarshaller`类将 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 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 type jaxbUnmarshaller.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(); } } } ``` 程序输出: ```java Employee [id=1, firstName=Lokesh, lastName=Gupta, department=Department [id=101, name=IT]] ``` 读取的 JSON 文件为: ```java { "employee" : { "department" : { "id" : 101, "name" : "IT" }, "firstName" : "Lokesh", "id" : 1, "lastName" : "Gupta" } } ``` ## 4.将 JSON 字符串转换为 Java 对象 您可以获取字符串形式的 JSON,然后直接填充到 Java 对象。 ```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 type jaxbUnmarshaller.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(); } } } ``` 程序输出: ```java Employee [id=1, firstName=Lokesh, lastName=Gupta, department=Department [id=101, name=IT]] ``` ## 5.将 Java 对象转换为 JSON 将 Java 对象转换为 JSON 的示例。 ```java 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 JSON jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); //Set JSON type jaxbMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json"); jaxbMarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true); //Print JSON String to Console StringWriter sw = new StringWriter(); jaxbMarshaller.marshal(employee, sw); System.out.println(sw.toString()); } catch (JAXBException e) { e.printStackTrace(); } } } ``` 程序输出: ```java { "employee" : { "department" : { "id" : 101, "name" : "IT" }, "firstName" : "Lokesh", "id" : 1, "lastName" : "Gupta" } } ``` > 阅读更多:[将 Java 对象转换为 XML](https://howtodoinjava.com/jaxb/write-object-to-xml/) 在评论部分中,向我发送您与编组和解组 json 有关的问题。 学习愉快!