java json字符串转对象_json转换为java对象_ json字符串转对象数组使用JAXB
教程分享
>
WEB技术
>
JavaScript
(2567)
2024-08-07 11:21:12
将JSON转换为Java Object的Java示例。您可以将
JSON String解组
为Object或将
JSON文件解组
为Object。此示例使用MOXy和JAXB将JSON解组为Java对象。MOXy实现了JAXB,允许开发人员通过注释提供他们的映射信息,并提供许多JAXB默认不提供的丰富功能。
1)MOXy依赖
将MOXy包含在项目运行时中。
pom.xml |
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
<version>2.5.2</version>
</dependency>
|
2)JSON文件到Java对象
2.1)添加JAXB注释
Employee.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
}
|
Department.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
文件并构造上下文。在这里,您JAXBContextFactory
从MOXy库中注入。 将jaxb.properties
文件放在放置JAXB注释类的同一个包中。
jaxb.properties |
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
|
2.3)将JSON转换为Object
现在使用javax.xml.bind.UnMarshaller
class将json转换为object。
JaxbExample.java |
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();
}
}
}
|
输出结果:
Employee [id=1, firstName=Lokesh, lastName=Gupta, department=Department [id=101, name=IT]]
要读取的JSON文件是:
employee.json |
{
"employee" : {
"department" : {
"id" : 101,
"name" : "IT"
},
"firstName" : "Lokesh",
"id" : 1,
"lastName" : "Gupta"
}
}
|
3)将JSON字符串转换为Java对象
您可以以STring形式获取JSON,然后直接填充到Java对象。
JaxbExample.java |
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();
}
}
}
|
输出:
Employee [id=1, firstName=Lokesh, lastName=Gupta, department=Department [id=101, name=IT]]
4)Java对象到JSON
将Java对象转换为JSON的示例。
JAXBExample.java |
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();
}
}
}
|
输出:
{
"employee" : {
"department" : {
"id" : 101,
"name" : "IT"
},
"firstName" : "Lokesh",
"id" : 1,
"lastName" : "Gupta"
}
}
https://www.leftso.com/article/476.html