该JSON杰克逊是一个Java库。它具有非常强大的数据绑定功能,并提供了一个框架,用于将自定义Java对象序列化为JSON并将JSON反序列化为Java对象。我们还可以使用XmlMapper 类的readValue()方法将XML格式转换为POJO对象。
public <T> T readValue(XMLStreamReader r, Class<T> valueType) throws IOException
import com.fasterxml.jackson.dataformat.xml.*; public class XMLToPOJOTest { public static void main(String args[]) throws Exception { try { XmlMapper xmlMapper = new XmlMapper(); Person pojo = xmlMapper.readValue(getXmlString(), Person.class); System.out.println(pojo); } catch(Exception e) { e.printStackTrace(); } } private static String getXmlString() { return "<person> <firstName>Adithya</firstName>" + "<lastName>Jai</lastName>" + "<address>Bangalore</address>" + "</person>"; } }// Person class (POJO)class Person { private String firstName; private String lastName; private String address; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String toString() { return "Person[ " + "firstName = " + firstName + ", lastName = " + lastName + ", address = " + address + " ]"; } }
输出结果
Person[ firstName = Adithya, lastName = Jai, address = Bangalore ]