<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.28</version> </dependency>
public class User {
private String name;
private Integer age;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
@Test public void testJson(){ //bean 2 json User user = new User(); user.setName("danny"); user.setAge(11); user.setSex("male"); logger.info(JSON.toJSONString(user)); //json 2 bean String jsonStr = "{"age":88,"name":"danny11","sex":"male"}"; User danny = JSON.parseObject(jsonStr, User.class); logger.info("danny--->" + danny.getName()); logger.info("danny--->" + danny.getAge()); logger.info("danny--->" + danny.getSex()); }