
Easily find JSON paths within JSON objects using our intuitive Json Path Finder
30
json-lib 是一个 java 库,用于序列化和反序列化 json 格式的 java beans、映射、数组和集合。我们可以使用 xmlserializer 类的 settypehintsenabled() 方法将 bean 转换为没有类型提示的 xml,该方法设置是否可以将 json 类型作为属性包含在内。我们可以将 false 作为参数传递给此方法,以禁用 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>
<!--?xml version="1.0" encoding="UTF-8"?-->以上就是如何在Java中使用JSON-lib API将bean转换为没有类型提示的XML?的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号