原文: 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 到项目运行时。

  1. <dependency>
  2. <groupId>org.eclipse.persistence</groupId>
  3. <artifactId>org.eclipse.persistence.moxy</artifactId>
  4. <version>2.5.2</version>
  5. </dependency>

2.将 JSON 文件转换为 Java 对象

2.1 添加 JAXB 注解

  1. @XmlRootElement(name = "employee")
  2. @XmlAccessorType(XmlAccessType.PROPERTY)
  3. public class Employee implements Serializable {
  4. private static final long serialVersionUID = 1L;
  5. private Integer id;
  6. private String firstName;
  7. private String lastName;
  8. private Department department;
  9. public Employee() {
  10. super();
  11. }
  12. //Setters and Getters
  13. }
  1. @XmlRootElement(name = "department")
  2. @XmlAccessorType(XmlAccessType.PROPERTY)
  3. public class Department implements Serializable {
  4. private static final long serialVersionUID = 1L;
  5. Integer id;
  6. String name;
  7. public Department() {
  8. super();
  9. }
  10. //Setters and Getters
  11. }

2.2 添加jaxb.properties

当您获得JAXBContext的实例时,JAXB 将检查jaxb.properties文件并构造上下文。 在这里,您从 MOXy 库中注入了JAXBContextFactory

jaxb.properties文件放在放置 JAXB 注解类的同一包中。

  1. javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

3.将 JSON 文件转换为对象

现在使用javax.xml.bind.UnMarshaller类将 json 转换为对象。

  1. package com.howtodoinjava.demo;
  2. import java.io.File;
  3. import javax.xml.bind.JAXBContext;
  4. import javax.xml.bind.JAXBException;
  5. import javax.xml.bind.Marshaller;
  6. import javax.xml.bind.Unmarshaller;
  7. import org.eclipse.persistence.jaxb.MarshallerProperties;
  8. import org.eclipse.persistence.jaxb.UnmarshallerProperties;
  9. import com.howtodoinjava.demo.model.Department;
  10. import com.howtodoinjava.demo.model.Employee;
  11. public class JaxbExample
  12. {
  13. public static void main(String[] args)
  14. {
  15. String fileName = "employee.json";
  16. jaxbJsonToObject(fileName);
  17. }
  18. private static void jaxbJsonToObject(String fileName) {
  19. File xmlFile = new File(fileName);
  20. JAXBContext jaxbContext;
  21. try
  22. {
  23. jaxbContext = JAXBContext.newInstance(Employee.class);
  24. Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
  25. //Set JSON type
  26. jaxbUnmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
  27. jaxbUnmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, true);
  28. Employee employee = (Employee) jaxbUnmarshaller.unmarshal(xmlFile);
  29. System.out.println(employee);
  30. }
  31. catch (JAXBException e)
  32. {
  33. e.printStackTrace();
  34. }
  35. }
  36. }

程序输出:

  1. Employee [id=1, firstName=Lokesh, lastName=Gupta, department=Department [id=101, name=IT]]

读取的 JSON 文件为:

  1. {
  2. "employee" : {
  3. "department" : {
  4. "id" : 101,
  5. "name" : "IT"
  6. },
  7. "firstName" : "Lokesh",
  8. "id" : 1,
  9. "lastName" : "Gupta"
  10. }
  11. }

4.将 JSON 字符串转换为 Java 对象

您可以获取字符串形式的 JSON,然后直接填充到 Java 对象。

  1. package com.howtodoinjava.demo;
  2. import java.io.File;
  3. import javax.xml.bind.JAXBContext;
  4. import javax.xml.bind.JAXBException;
  5. import javax.xml.bind.Marshaller;
  6. import javax.xml.bind.Unmarshaller;
  7. import org.eclipse.persistence.jaxb.MarshallerProperties;
  8. import org.eclipse.persistence.jaxb.UnmarshallerProperties;
  9. import com.howtodoinjava.demo.model.Department;
  10. import com.howtodoinjava.demo.model.Employee;
  11. public class JaxbExample
  12. {
  13. public static void main(String[] args)
  14. {
  15. String jsonString = "{\"employee\":{\"department\":{\"id\":101,\"name\":\"IT\"},
  16. \"firstName\":\"Lokesh\",\"id\":1,\"lastName\":\"Gupta\"}}";
  17. jaxbJsonStringToObject(jsonString);
  18. }
  19. private static void jaxbJsonStringToObject(String jsonString)
  20. {
  21. JAXBContext jaxbContext;
  22. try
  23. {
  24. jaxbContext = JAXBContext.newInstance(Employee.class);
  25. Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
  26. //Set JSON type
  27. jaxbUnmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
  28. jaxbUnmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, true);
  29. Employee employee = (Employee) jaxbUnmarshaller.unmarshal(new StringReader(jsonString));
  30. System.out.println(employee);
  31. }
  32. catch (JAXBException e)
  33. {
  34. e.printStackTrace();
  35. }
  36. }
  37. }

程序输出:

  1. Employee [id=1, firstName=Lokesh, lastName=Gupta, department=Department [id=101, name=IT]]

5.将 Java 对象转换为 JSON

将 Java 对象转换为 JSON 的示例。

  1. package com.howtodoinjava.demo;
  2. import java.io.StringWriter;
  3. import javax.xml.bind.JAXBContext;
  4. import javax.xml.bind.JAXBException;
  5. import javax.xml.bind.Marshaller;
  6. import org.eclipse.persistence.jaxb.MarshallerProperties;
  7. import com.howtodoinjava.demo.model.Department;
  8. import com.howtodoinjava.demo.model.Employee;
  9. public class JaxbExample
  10. {
  11. public static void main(String[] args)
  12. {
  13. Employee employee = new Employee(1, "Lokesh", "Gupta", new Department(101, "IT"));
  14. jaxbObjectToJSON(employee);
  15. }
  16. private static void jaxbObjectToJSON(Employee employee)
  17. {
  18. try
  19. {
  20. JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
  21. Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
  22. // To format JSON
  23. jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
  24. //Set JSON type
  25. jaxbMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
  26. jaxbMarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);
  27. //Print JSON String to Console
  28. StringWriter sw = new StringWriter();
  29. jaxbMarshaller.marshal(employee, sw);
  30. System.out.println(sw.toString());
  31. }
  32. catch (JAXBException e)
  33. {
  34. e.printStackTrace();
  35. }
  36. }
  37. }

程序输出:

  1. {
  2. "employee" : {
  3. "department" : {
  4. "id" : 101,
  5. "name" : "IT"
  6. },
  7. "firstName" : "Lokesh",
  8. "id" : 1,
  9. "lastName" : "Gupta"
  10. }
  11. }

阅读更多:将 Java 对象转换为 XML

在评论部分中,向我发送您与编组和解组 json 有关的问题。

学习愉快!