1.详细介绍可以查看github地址:https://github.com/alibaba/fastjson,
阿里自称在服务端及安卓端能提供最好的性能、提供一个方便简单的对象与json相互转化的api、Allow pre-existing unmodifiable objects to be converted to and from JSON(这句翻译不出来)、广泛的支持java泛型、允许对象的自定义表示、支持任意复杂对象。
2.我把他们官方给的实例也搬出来。
public class Group {
private Long id;
private String name;
private List<User> users = new ArrayList<User>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public void addUser(User user) {
users.add(user);
}
@Override
public String toString() {
return "Group [id=" + id + ", name=" + name + ", users=" + users + "]";
}
}
public class User {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Group group = new Group();
group.setId(0L);
group.setName("admin");
User guestUser = new User();
guestUser.setId(2L);
guestUser.setName("guest");
User rootUser = new User();
rootUser.setId(3L);
rootUser.setName("root");
group.addUser(guestUser);
group.addUser(rootUser);
String jsonString = JSON.toJSONString(group);
System.out.println(jsonString);
Group group2 = JSON.parseObject(jsonString, Group.class);
System.out.println(group2);
System.out.println(JSON.toJSONStringWithDateFormat(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));