前言

/** * JSON字符串转换为对象 * @param src JSON字符串 * @param target 目标Bean */ public static <T> T String2Object(String src, Class<T> target) throws JsonParseException, JsonMappingException, IOException { ObjectMapper mapper = new ObjectMapper(); //配置,允许使用单引号字符的JSON mapper.configure(JsonParser.Feature. ALLOW_SINGLE_QUOTES, true); return mapper.readValue( new StringReader(src), target); }
2.Bean对象转换为JSON字符串:
/*** * 将对象序列化为JSONObject字符串 * @param object bean对象 * @return String JSONString */ public static String toJSONObjectString(Object object) { JSONObject jsonObject = JSONObject. fromObject(object); return jsonObject.toString(); }
3.JSON字符串转换为ArrayList
/** * 字符串转化为目标List列表 * @param src 源JSON串 * @param target 目标类型 * @return 转化后的列表对象 */ @SuppressWarnings("rawtypes") public static List String2ObjectsList(String src, Class target) throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.configure(JsonParser.Feature. ALLOW_SINGLE_QUOTES, true); List result = mapper.readValue( new StringReader(src), TypeFactory. collectionType(ArrayList.class, target)); return result; }
4.ArrayList转换为JSON字符串
/** * 将List对象序列化为JSON文本 * @param list * @return List <T> */ public static <T> String toJSONString(List<T> list) { JSONArray jsonArray = JSONArray. fromObject(list); return jsonArray.toString(); }
其他的一些复杂的Bean转换,就不再多介绍了,主要都是围绕JSONObject,JSONArray,ObjectMapper这几个对象操作的,具体可以看看官方文档。
import org.simpleframework.xml.Attribute; import org.simpleframework.xml.Element; import org.simpleframework.xml.Root; @Root//根节点 public class User { @Attribute//作为属性 private Integer id; @Element//作为元素 private String name; @Element private String email; public User() { super(); } public User(Integer id, String name, String email) { super(); this. id = id; this. name = name; this. email = email; } /** * the below is getter and setter function */ public Integer getId() { return id; } public void setId(Integer id) { this. id = id; } public String getName() { return name; } public void setName(String name) { this. name = name; } public String getEmail() { return email; } public void setEmail(String email) { this. email = email; } }
1.XML字符串转换为Bean对象
//定义序列化对象 Serializer serializer = new Persister(); User user = new User(1001,"test","wewoor@foxmail.com" ); String test = "<user id='1001'><name>test12</name><email>wewoor@foxmail.com</email></user>"; //读取 serializer.read(user, test); System. out.print(user.getId());
2.Bean对象转换为XML字符串
User user = new User(1001,"test","wewoor@foxmail.com" ); //定义序列化对象 Serializer serializer = new Persister(); //输出流 OutputStream out = new ByteArrayOutputStream(); serializer.write(user, out); result = out.toString(); System. out.print(result); out.close();
3.将Bean对象写入XML文件
file = new File("Ziv.xml"); User user = new User(1001,"test","wewoor@foxmail.com" ); //定义序列化对象 Serializer serializer = new Persister(); //将User user 写入ziv.xml serializer.write(user, file);
4.读取XML文件转换为Bean
file = new File( "Ziv.xml"); User user = new User(); //定义序列化对象 Serializer serializer = new Persister(); //读取ziv.xml到User中 serializer.read(user, file);
转载于:https://www.cnblogs.com/MMLoveMeMM/articles/3606656.html
8075




被折叠的 条评论
为什么被折叠?



