FastJson中JSON,JSONObject和JSONArray简单使用

JSON中提供了常用的对象和json数据间的转换方法。
JSONObject可以看作Map,使用key-value构建json对象。
JSONArray可以看作List,使用List可以简单构造json数组对象。

1.pom.xml中引入依赖

   com.alibaba.fastjson2
   fastjson2
   2.0.29
2.准备实体类
import com.alibaba.fastjson2.annotation.JSONField;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.util.Date;

@Slf4j
@Data
public class Person {
    @JSONField(name="AGE", serialize=false)
    private int age;

    @JSONField(name="LAST NAME", ordinal = 2)
    private String lastName;

    @JSONField(name="FIRST NAME", ordinal = 1)
    private String firstName;

    @JSONField(name="DATE OF BIRTH", format="dd/MM/yyyy", ordinal = 3)
    private Date dateOfBirth;

    public Person() {
    }
    public Person(int age, String lastName, String firstName, Date dateOfBirth) {
        this.age = age;
        this.lastName = lastName;
        this.firstName = firstName;
        this.dateOfBirth = dateOfBirth;
    }
}
3.准备测试类
import com.test.fastjson.to.Person;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.filter.NameFilter;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

@Slf4j
class TestTest {
    private List listOfPersons = new ArrayList<>();

    @BeforeEach
    public void setUp() {
        listOfPersons.add(new Person(15, null,"Doe", new Date()));
        listOfPersons.add(new Person(20, "Janette Doe", "Kobe", new Date()));
    }

    /**
     * convert object to json String.
     */
    @Test
    public void whenJavaList_thanConvertToJsonCorrect() {
        String jsonOutput= JSON.toJSONString(listOfPersons);
        log.info(jsonOutput);
    }

    /**
     * json string to object
     */
    @Test
    public void whenJson_thanConvertToObjectCorrect() {
        Person person = new Person(20, "John", "Doe", new Date());
        String jsonObject = JSON.toJSONString(person);
        Person newPerson = JSON.parseObject(jsonObject, Person.class);

        assertEquals(newPerson.getAge(), 0); // 如果我们设置系列化为 false
        assertEquals(newPerson.getFirstName(), listOfPersons.get(0).getFirstName());
    }

    /**
     * how to use JSONObject and JSONArray
     */
    @Test
    public void whenGenerateJson_thanGenerationCorrect() {
        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();
        log.info(jsonOutput);
    }

    /**
     * define NameFilter
     */
    @Test
    public void givenSerializeConfig_whenJavaObject_thanJsonCorrect() {
        NameFilter formatName = new NameFilter() {
            public String process(Object object, String name, Object value) {
                return name.toLowerCase().replace(" ", "_");//FIRST NAME  ---> first_name
            }
        };
        String jsonOutput = JSON.toJSONString(listOfPersons, formatName);
        log.info(jsonOutput);
    }

}

你可能感兴趣的:(json)