Preface in web or other projects, JSON and XML data are two types of data that people often encounter. JSON and XML formats are also basic data transfer formats when data is docked with various platforms. This document briefly introduces the reading and writing operations for JSON/XML files, and the conversion with Bean. The sample code has been tested in Demo mode. Of course, the code will also be uploaded to GitHub. You can check it out if you need it. Preparation I mainly rely on the following two open source libraries for XML and JSON. Of course, there are many open source libraries for xml and JSON parsing, the following two feelings are relatively simple to use. Note that if one of the following jar files is missing, the ClassNotFound exception may occur. If the method used cannot be found in the current library, it may be caused by different library versions, visit the official website. JSON operation code: 1. JSON string to Bean 1/** 2 * JSON string to object 3 * @ param src JSON string 4 * @ param target Bean 5 */6 public static <T> T string2Object (String src, class <T> target) throws 7 JsonParseException, JsonMappingException, IOException {8 9 ObjectMapper mapper = new ObjectMapper (); 10 // configuration, JSON11 mapper with single quotation marks is allowed. configure (JsonParser. feature. ALLOW_SINGLE_QUOTES, true); 12 return mapper. readValue (new StringReader (src), target); 13} 2. bean object to JSON String 1/*** 2 * serialize object to JSONObject String 3 * @ param object bean object 4 * @ return String JSONString5 */6 public static String toJSONObjectString (object object) {7 JSONObject jsonObject = JSONObject. fromObject (object); 8 return jsonObject. toString (); 9} 3. JSON string converted to ArrayList 1/** 2 * string converted to target List 3 * @ param src Source JSON string 4 * @ param target type 5 * @ return converted List pair Like 6 */7 @ SuppressWarnings ("rawtypes") 8 public static List String2ObjectsList (String src, Class target) 9 throws Exception {10 11 ObjectMapper mapper = new ObjectMapper (); 12 mapper. configure (JsonParser. feature. ALLOW_SINGLE_QUOTES, true); 13 List result = mapper. readValue (new StringReader (src), 14 TypeFactory. collectionType (ArrayList. class, target); 15 16 return result; 17} 4. arrayList to JSON string 1/** 2 * serialize the List object to JSON Text 3 * @ param list 4 * @ return List <T> 5 */6 public static <T> String toJSONString (List <t> list) {7 8 JSONArray jsonArray = JSONArray. fromObject (list); 9 return jsonArray. toString (); 10} other complicated Bean conversions will not be described much. They mainly focus on operations on JSONObject, JSONArray, and ObjectMapper objects. For details, refer to the official documentation. Detailed code for XML operations: the Simple library converts Bean and XML by adding annotations to Bean objects in a Simple and easy-to-understand manner. Example: 1 import org. simpleframework. xml. attribute; 2 import org. simpleframework. xml. element; 3 import org. simpleframework. xml. root; 4 5 @ Root // Root node 6 public class User {7 8 @ Attribute // as Attribute 9 private Integer id; 10 11 @ Element // As Element 12 private String name; 13 14 @ Element15 private String email; 16 17 public User () {18 super (); 19} 20 21 public User (Integer id, String name, String email) {22 super (); 23 this. id = id; 24 this. name = name; 25 this. email = email; 26} 27 28/** 29 * the below is getter and setter function30 */31 32 public Integer getId () {33 return id; 34} 35 36 public void setId (Integer id) {37 this. id = id; 38} 39 40 public String getName () {41 return name; 42} 43 44 public void setName (String name) {45 this. name = name; 46} 47 48 public String getEmail () {49 return email; 50} 51 52 public void setEmail (String email) {53 this. email = email; 54} 55} 1. XML string to Bean object 1 // define serialized object 2 Serializer serializer = new Persister (); 3 User user = new User (1001, "test", "wewoor@foxmail.com "); 4 String test = "<user id = '000000'> <name> test12 </name> <email> wewoor@foxmail.com </email> </user> "; 5 // read 6 serializer. read (user, test); 7 System. out. print (user. getId (); 2. bean object to XML string 1 User user = new User (1001, "test", "wewoor@foxmail.com"); 2 // define serialized object 3 Serializer serializer = new Persister (); 4 // output stream 5 OutputStream out = new ByteArrayOutputStream (); 6 serializer. write (user, out); 7 result = out. toString (); 8 System. out. print (result); 9 out. close (); 3. write Bean object to XML file 1 File = new file ("Ziv. xml "); 2 User user = new User (1001," test "," wewoor@foxmail.com "); 3 // define the serialized object 4 Serializer serializer = new Persister (); 5 // write the User into ziv. xml6 serializer. write (user, file); 4. read the XML file and convert it to Bean 1 File = new file ("Ziv. xml "); 2 User user = new User (); 3 // define the serialization object 4 Serializer serializer = new Persister (); 5 // read ziv. xml to User 6 serializer. read (user, file );