java中将javabean转为json的两种第三方工具类FastjsonJackson
java中将javabean转为json的两种第三方工具类Fastjson&&Jackson
一、Fastjson
1、导入jar包
2.实体类user
public class User {
private int id;
private String name;
private int age;
//对Date类型进行格式化输出
@JSONField(format="yyyy-MM-dd")
private Date birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
3、转换的例子
public class FastjsonTest {
// 将User对象转换成json
@Test
public void test1() {
User user = new User();
user.setAge(20);
user.setBirthday(new Date());
user.setId(1);
user.setName("tom");
// 处理属性在json中是否生成
SerializeFilter filter = new PropertyFilter() {
@Override
public boolean apply(Object arg0, String arg1, Object arg2) {
// System.out.println(arg0); //要转换成json的对象
// System.out.println(arg1); //属性名称
// System.out.println(arg2); //属性值
if (arg1.equals("id")) {
return false; // 代表不生成在json串中
}
return true; // 代表生成在json串中
}
};
// 转换成json
String json = JSONObject.toJSONString(user, filter);
System.out.println(json);
// {"age":20,"birthday":1479455891302,"id":1,"name":"tom"}
}
// 将List转换成json
@Test
public void test2() {
User u1 = new User();
u1.setAge(20);
u1.setBirthday(new Date());
u1.setId(1);
u1.setName("tom");
User u2 = new User();
u2.setAge(20);
u2.setBirthday(new Date());
u2.setId(1);
u2.setName("fox");
List users = new ArrayList();
users.add(u1);
users.add(u2);
String json = JSONArray.toJSONString(users);
System.out.println(json);
// [{"age":20,"birthday":1479456003742,"id":1,"name":"tom"},{"age":20,"birthday":1479456003742,"id":1,"name":"fox"}]
}
}
二、Jackson
Spring mvc它的底层使用的就是jackson
1.导入jar包
2.使用
//@JsonIgnoreProperties({ "id", "releaseDate" })
//@JsonFilter("productFilter")
public class Product {
// @JsonIgnore
private int id;
private String name;
private double price;
// @JsonIgnore
private Date releaseDate;// 出生日期
// @JSON(serialize = false)
public Date getReleaseDate() {
return releaseDate;
}
public void setReleaseDate(Date releaseDate) {
this.releaseDate = releaseDate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
3、整个操作过程
public class JacksonTest {
// 将Product转换成json
@Test
public void test1() throws JsonGenerationException, JsonMappingException, IOException {
Product p = new Product();
p.setId(1);
p.setName("电视机");
p.setPrice(2000);
p.setReleaseDate(new Date());
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd")); // 设置日期格式化器
String json = mapper.writeValueAsString(p);
System.out.println(json);
}
// 将List转换成json
@Test
public void test2() throws JsonGenerationException, JsonMappingException, IOException {
Product p1 = new Product();
p1.setId(1);
p1.setName("电视机");
p1.setPrice(2000);
Product p2 = new Product();
p2.setId(2);
p2.setName("电冰箱");
p2.setPrice(3000);
List ps = new ArrayList();
ps.add(p1);
ps.add(p2);
ObjectMapper mapper = new ObjectMapper();
// 处理过滤属性
// FilterProvider fp = new SimpleFilterProvider().addFilter("productFilter",
// SimpleBeanPropertyFilter.filterOutAllExcept("id", "name")); //只包含id与name
FilterProvider fp = new SimpleFilterProvider().addFilter("productFilter",
SimpleBeanPropertyFilter.serializeAllExcept("id", "name")); //不包含id与name
mapper.setFilters(fp);
String json = mapper.writeValueAsString(ps);
System.out.println(json);
}
}
java中将javabean转为json的两种第三方工具类FastjsonJackson相关教程