前言php
1 /** 2 * JSON字符串轉換爲對象 3 * @param src JSON字符串 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 //配置,容許使用單引號字符的JSON 11 mapper.configure(JsonParser.Feature. ALLOW_SINGLE_QUOTES, true); 12 return mapper.readValue( new StringReader(src), target); 13 }
1 /*** 2 * 將對象序列化爲JSONObject字符串 3 * @param object bean對象 4 * @return String JSONString 5 */ 6 public static String toJSONObjectString(Object object) { 7 JSONObject jsonObject = JSONObject. fromObject(object); 8 return jsonObject.toString(); 9 }
1 /** 2 * 字符串轉化爲目標List列表 3 * @param src 源JSON串 4 * @param target 目標類型 5 * @return 轉化後的列表對象 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 }
1 /** 2 * 將List對象序列化爲JSON文本 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 }
1 import org.simpleframework.xml.Attribute; 2 import org.simpleframework.xml.Element; 3 import org.simpleframework.xml.Root; 4 5 @Root//根節點 6 public class User { 7 8 @Attribute//做爲屬性 9 private Integer id; 10 11 @Element//做爲元素 12 private String name; 13 14 @Element 15 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 function 30 */ 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 //定義序列化對象 2 Serializer serializer = new Persister(); 3 User user = new User(1001,"test","wewoor@foxmail.com" ); 4 String test = "<user id='1001'><name>test12</name><email>wewoor@foxmail.com</email></user>"; 5 //讀取 6 serializer.read(user, test); 7 System. out.print(user.getId());
1 User user = new User(1001,"test","wewoor@foxmail.com" ); 2 //定義序列化對象 3 Serializer serializer = new Persister(); 4 //輸出流 5 OutputStream out = new ByteArrayOutputStream(); 6 serializer.write(user, out); 7 result = out.toString(); 8 System. out.print(result); 9 out.close();
1 file = new File("Ziv.xml"); 2 User user = new User(1001,"test","wewoor@foxmail.com" ); 3 //定義序列化對象 4 Serializer serializer = new Persister(); 5 //將User user 寫入ziv.xml 6 serializer.write(user, file);
1 file = new File( "Ziv.xml"); 2 User user = new User(); 3 //定義序列化對象 4 Serializer serializer = new Persister(); 5 //讀取ziv.xml到User中 6 serializer.read(user, file);