1.介绍
这是阿里巴巴提供的jar,主要作用就是Java对象和json字符串之间的转换,不用自己编写复杂的逻辑,用起来是真的很方便。官网是这么介绍的:FastJson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean。 当然FastJson也有一些不足的地方,所以具体可以看这篇文章。 zhuanlan.zhihu.com/p/146540159
2.如何使用
话不多说,直接上代码
Person类
import com.alibaba.fastjson.annotation.JSONField;
import java.util.Date;
public class Person {
/**
* 注意:FastJson 在进行操作时,是根据 getter 和 setter 的方法进行的,并不是依据 Field 进行。
* 注意:若属性是私有的,必须有 set 方法。否则无法反序列化。
*/
@JSONField(name = "ID") //自定义输出时key值
private int id;
@JSONField(name = "NAME", serialize = false) //serialize = false不序列化
private String name;
@JSONField(name = "PHONE")
private String phone;
@JSONField(name = "MESSAGE")
private String message;
@JSONField(name = "BIRTH" , format = "YYYY-MM-dd") //format表示日期的显示方式
private Date birth;
public Person(){
}
public Person(int id, String name, String phone, String message, Date birth) {
this.id = id;
this.name = name;
this.phone = phone;
this.message = message;
this.birth = birth;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
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 String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + ''' +
", phone='" + phone + ''' +
", message='" + message + ''' +
", birth=" + birth +
'}';
}
}
测试代码
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.BeanContext;
import com.alibaba.fastjson.serializer.ContextValueFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.util.Date;
public class Test {
public static void main(String[] args) {
Person p = new Person(1, "张三" , "12334", null, new Date());
//转换为json字符串
String s = JSON.toJSONString(p); //为空的字段不输出
//转换为BeantoArray对象
String s1 = JSON.toJSONString(p, SerializerFeature.BeanToArray); //输出为数组
System.out.println(s);
System.out.println(s1);
System.out.println("----------------------------------------------------");
String t1 = "{"id":1,"name":"张三","phone":"12334","address":"上海"}"; // 属性名不对应的不映射
Person person = JSON.parseObject(t1, Person.class);
System.out.println(person);
System.out.println("----------------------------------------------------");
//通过Json对象创建Json字符串
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < 2; i++) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("AGE", 10);
jsonObject.put("FULL NAME", "Doe " + i);
jsonObject.put("DATE OF BIRTH", "2016/12/12 12:12:12");
jsonArray.add(jsonObject);
}
String jsonOutput = jsonArray.toJSONString();
System.out.println(jsonOutput);
System.out.println("----------------------------------------------------");
//对json字符串过滤
Person p1 = new Person(1, "Jhon", "123123", "hello", new Date());
ContextValueFilter valueFilter = (context, object, name, value) -> {
if (name.equals("BIRTH")) {
return "NOT TO DISCLOSE";
}
if (value.equals("hello")) {
return ((String) value).toUpperCase();
}
return value;
};
String t2 = JSON.toJSONString(p1 , valueFilter);
System.out.println(t2);
}
}
输出结果