1. First define a Java object Person:
public class Person{ String name; int age; int number; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", number=" + number + "]"; }}2. Using Alibaba's fastjson-1.2.13.jar (http://maven.outofmemory.cn/com.alibaba/fastjson/1.2.13/) toolkit, use the toJSONString (Object object) method of the JSON class to convert the Java object into a String json string. The code is as follows:
Person person = new Person(); person.setName("Old Four"); person.setAge(26); person.setNumber(16); // JSON object serialization String personJson = null; // Convert Java object to json personJson = JSON.toJSONString(person); System.out.println(personJson);3. Similarly, through this toolkit, you can convert the json string into a Java object and call the parseObject(String text, Class<T> clazz) method. You only need to pass in the json string and the class of the target Java object Person class. The code is as follows:
// Convert json to Java object Person parsePerson = JSON.parseObject(personJson, person.getClass()); System.out.println(parsePerson);
4. Using the xstream-1.4.9.jar (http://maven.outofmemory.cn/com.thoughtworks.xstream/xstream/1.4.9/) toolkit, you just need to create an XStream object and then use the toXML (Object obj) method to realize the conversion of Java objects to XML. The code is as follows:
Person person = new Person(); person.setName("Last Four"); person.setAge(26); person.setNumber(16); // Create XStream object first XStream xStream = new XStream(new DomDriver()); // Set the alias of the Person class xStream.alias("Last Four", Person.class); // Serialize Java objects into XML String personXML = xStream.toXML(person); // Output XML System.out.println(personXML);5. Similarly, through this toolkit, you can convert XML into Java objects, call the fromXML (String xml) method, and pass in the XML parameters of String type. The code is as follows:
// Convert XML to Java object Person parsePerson = (Person) xStream.fromXML(personXML); // Output Java object System.out.println(parsePerson);
illustrate:
XStream objects are equivalent to converters between Java objects and XML, and the conversion process is bidirectional. The way to create an XSteam object is very simple, you only need new XStream();
Java to xml, use the toXML() method.
Xml to Java, use the fromXML() method.
By default, the java to xml mapping is the element name corresponding to the java member name, and the full name of the java class corresponds to the name of the root element of the xml. In reality, there are often both XML and Java classes. To complete mutual conversion, alias mapping must be performed.
Alias configuration includes three cases:
1. Category name, use alias(String name, Class type).
2. Class member alias, use aliasField(String alias, Class definedIn, String fieldName)
3. Class members are used as attribute alias, and use aliasAttribute(Class definedIn, String attributeName, String alias). It is meaningless to name them separately. They must also be applied to a certain class through useAttributeFor(Class definedIn, String fieldName).
The above example of serializing Java objects into JSON and XML formats is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.