? Fastjson 是一个 Java 库,可以将 Java 对象转换为 JSON 格式,也可以将 JSON 字符串转换为 Java 对象。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.colobu</groupId>
<artifactId>fastjson-jaxrs-json-provider</artifactId>
<version>0.3.1</version>
</dependency>
import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
@JSONField(name="USERNAME",ordinal = 1)
private String username;
@JSONField(name="AGE",ordinal = 2)
private int age;
//排除不需要序列化的字段
@JSONField(serialize = false)
private String birthday;
}
import com.alibaba.fastjson.JSON;
import org.junit.Test;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
public class TestFastJSON {
//Java对象转JSON
@Test
public void javaBeanToJSON(){
//创建Person对象
Person p = new Person("张三",16, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
//使用JSON对象的 toString方法将对象转换为JOSN数据
String s = JSON.toJSONString(p);
System.out.println(s);
//{"age":16,"birthday":"2024-01-16 21:59:23","username":"张三"}
}
//List集合转Json
@Test
public void ListToJSON(){
//创建Person对象
Person p1 = new Person("张三",15, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
Person p2 = new Person("李四",16, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
Person p3 = new Person("王五",13, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
List<Person> list = new ArrayList<>();
Collections.addAll(list,p1,p2,p3);
//使用JSON对象的 toString方法将对象转换为JOSN数据
String s = JSON.toJSONString(list);
System.out.println(s);
//[{"age":15,"birthday":"2024-01-16 22:00:05","username":"张三"},{"age":16,"birthday":"2024-01-16 22:00:05","username":"李四"},{"age":13,"birthday":"2024-01-16 22:00:05","username":"王五"}]
}
}
@Data
public class Person {
//自定义输出的名称, 并且进行输出排序
@JSONField(name="USERNAME",ordinal = 1)
private String username;
@JSONField(name="AGE",ordinal = 2)
private int age;
//排除不需要序列化的字段
@JSONField(serialize = false)
private String birthday;
public Person() {
}
public Person(String username, int age, String birthday) {
this.username = username;
this.age = age;
this.birthday = birthday;
}
}
import com.alibaba.fastjson.JSON;
import org.junit.Test;
import java.util.List;
public class JSONToJavaBean {
//JSON转Java对象
@Test
public void JSONToJavaBeanTest(){
String json = "{\"age\":12,\"birthday\":\"2024-01-01 10:00:00\",\"username\":\"张三\"}";
Person person = JSON.parseObject(json, Person.class);
System.out.println(person);
System.out.println("===================================");
//创建Person对象
String json2 ="[{\"age\":15,\"birthday\":\"2024-01-16 22:00:05\",\"username\":\"张三\"},{\"age\":16,\"birthday\":\"2024-01-16 22:00:05\",\"username\":\"李四\"},{\"age\":13,\"birthday\":\"2024-01-16 22:00:05\",\"username\":\"王五\"}]";
List<Person> list = JSON.parseArray(json2,Person.class);
System.out.println(list);
}
}