JSON-lib 자바 빈, 맵, 배열 및 컬렉션을 JSON 형식으로 직렬화 및 역직렬화하기 위한 Java 라이브러리입니다. 유형 힌트 없이 빈을 XML로 변환할 수 있습니다. setTypeHintsEnabled() 사용 XMLSerializer 클래스의 메서드인 이 메서드는 JSON 유형을 속성으로 포함할 수 있는지 여부를 설정합니다. 거짓을 전달할 수 있습니다. XML에서 유형 힌트를 비활성화하려면 이 메서드에 대한 인수로 사용합니다.
구문
public void setTypeHintsEnabled(boolean typeHintsEnabled)
예시
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
public class ConvertBeanToXMLNoHintsTest {
public static void main(String[] args) {
Employee emp = new Employee("Krishna Vamsi", 115, 30, "Java");
JSONObject jsonObj = JSONObject.fromObject(emp);
System.out.println(jsonObj.toString(3)); //pretty print JSON
XMLSerializer xmlSerializer = new XMLSerializer();
xmlSerializer.setTypeHintsEnabled(false); // this method disable type hints
String xml = xmlSerializer.write(jsonObj);
System.out.println(xml);
}
public static class Employee {
private String empName, empSkill;
private int empId, age;
public Employee(String empName, int empId, int age, String empSkill) {
super();
this.empName = empName;
this.empId = empId;
this.age = age;
this.empSkill = empSkill;
}
public String getEmployeeName() {
return empName;
}
public int getEmployeeId() {
return empId;
}
public String getEmployeeSkill() {
return empSkill;
}
public int getAge() {
return age;
}
}
} 출력
{
"employeeName": "Krishna Vamsi",
"employeeSkill": "Java",
"employeeId": 115,
"age": 30
}
<?xml version="1.0" encoding="UTF-8"?>
<o>
<age>30</age>
<employeeId>115</employeeId>
<employeeName>Krishna Vamsi</employeeName>
<employeeSkill>Java</employeeSkill>
</o>