1,导包fastjson
2,定义一个JavaBean 标明json注解
@JacksonXmlRootElement
public class User {
private String name;
@JacksonXmlProperty(localName="Name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
3,实现 java bean ———> xml(第一种)
User user = new User();
user.setName("AA");
user.setAge(10);
System.out.println(new XmlMapper().writeValueAsString(user));
(第二种)导包com.thoughtworks.xstream
XStream x=new XStream();
x.alias("User", User.class);
x.aliasField("Name", User.class, "name");
System.out.println(x.toXML(user));
4,实现 java bean ————> json
System.out.println(new ObjectMapper().writeValueAsString(user));
5,实现 json ————> java bean
String json = "{\"name\":\"zhangsan\",\"age\":10,\"sex\":\"man\",\"nickName\":\"张三\"}"; User user1=new ObjectMapper().readValue(json, User.class); System.out.println(user1.toString());
6,实现 xml ————> java bean
XStream x=new XStream();
String xml=x.toXML(user);
System.out.println(new ObjectMapper().readValue(xml, User.class));
结果就是
网友评论