Various type conversions of ultra-complete FastJson applications

  1. FastJson introduction

Fastjson is a high-performance JSON library written in Java language. It adopts an algorithm of “assuming ordered and fast matching” to improve the performance of JSON Parse to the extreme. It is currently the fastest JSON library in the Java language. The Fastjson interface is simple and easy to use, and has been widely used in various application scenarios such as cache serialization, protocol interaction, Web output, and Android client.

  1. Summary of various conversions (with code)

In daily development, you will often encounter conversions between Json and various types. Today, I will summarize all the conversions you can think of. There is no order, write what comes to mind. Create a new entity class that will be used later. I used lombok to be lazy, and the data is all written in vain, so don’t get entangled.

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class Person {
    private String name;
    private Integer age;
    private Date birth;
}

2.1 JavaBean to JsonObject

@SpringBootTest
public class JavaBean2JsonObjectTests {
    @Autowired
    private Person person;
    //JavaBean to JsonObject
    @Test
    void test(){
        person.setName("Zhang San");
        person. setAge(58);
        person.setBirth(new Date(19920320L));
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(person);
        System.out.println(jsonObject.get("name") + "/" + jsonObject.get("age"));
    }
}

The output is:

2.2 JavaBean to Json string

@SpringBootTest
public class JavaBean2JsonTests {
    @Autowired
    private Person person;
    //JavaBean to Json
    @Test
    void test(){
        person.setName("Zhang San");
        person. setAge(58);
        person.setBirth(new Date(19920327L));
        String jsonStr = JSON.toJSONString(person);
        System.out.println(jsonStr);
    }

The output is:

2.3Json string to JavaBean

@SpringBootTest
public class Json2JavaBeanTests {
    @Autowired
    private Person person;
    //Json to JavaBean
    @Test
    void test(){
        person.setName("Zhang San");
        person. setAge(58);
        person. setBirth(new Date());
        String jsonStr = JSON.toJSONString(person);
        System.out.println(jsonStr);
        Person person = JSONObject. parseObject(jsonStr, Person. class);
        System.out.println(person);
    }
}

The output is:

Note here that during serialization and deserialization, the Person object will be converted by default as a timestamp of Date type.

2.4 Json string to JsonObject

@SpringBootTest
public class Json2JsonObjectTests {
    //Json string to JsonObject
    @Test
    void test(){
        String jsonStr = "{"key1": 1,"key2":2,"key3":3}";
        JSONObject parse = JSONObject. parseObject(jsonStr);
        System.out.println(parse.getString("key1"));
    }
}

The output is: 1

2.5 Json string to List

@SpringBootTest
public class Json2ListTests {
    //Json to List
    @Test
    void test(){
        String jsonStr = "[{"key1": 1},{"key2":2},{"key3":3}]";
        List<Map> maps = JSONArray. parseArray(jsonStr, Map. class);
        maps.forEach(System.out::println);
    }
}

The output is:

2.6 Json string to Map

@SpringBootTest
public class Json2MapTests {
    @Test
    void test(){
        String jsonStr = "{"key1": 1,"key2":2,"key3":3}";
        Map map = JSONObject. parseObject(jsonStr, Map. class);
        map.forEach((o1, o2) -> System.out.println(o1 + "=" + o2));
    }
}

The output is:

2.7Json array string to JsonArray

@SpringBootTest
public class JsonArrayTests {
    //JSONArray add Object
    @Test
    void test() {
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(new Person("Zhang San", 76, new Date()));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("key1",1);
        jsonArray. add(jsonObject);
        String str = JSONArray.toJSONString(jsonArray);
        System.out.println(str);
    }
    //Json array string to JsonArray
    @Test
    void test1(){
        String jsonArrStr = "[{"name" : "Zhang San","age" : 26},{"name" : "Li Si","age" : 26} ]";
        JSONArray jsonArray = JSONArray. parseArray(jsonArrStr);
        jsonArray.forEach(o -> System.out.println(o.toString()));
    }
}

The output is:

test:

test1:

Note here that JsonArray is a class provided by FastJson. It is an object, not an array in the general sense we created. In the code, we can also see that JsonArray can add various Objects such as JsonObject and JavaBean.

2.8JsonObject to Json string

@SpringBootTest
public class JsonObject2JsonTests {
    //JsonObject to Json string
    @Test
    void test() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("key1", new Person("Zhang San", 11, new Date()));
        String jsonStr = JSON.toJSONString(jsonObject);
        System.out.println(jsonStr);
    }
}

The output is:

2.9List to JsonArray

@SpringBootTest
public class List2JsonArrayTests {
    @Autowired
    private Person person;
    //List to JsonArray
    @Test
    void test(){
        List<Person> list = new ArrayList<>();
        person.setName("Zhang San");
        person. setAge(26);
        person. setBirth(new Date());
        list. add(person);
        person = new Person();
        person.setName("Li Si");
        person. setAge(31);
        person. setBirth(new Date());
        list. add(person);
        JSONArray jsonArray = (JSONArray)JSONArray.toJSON(list);
        jsonArray.forEach(o -> System.out.println(o.toString()));
    }
}

The output is:

2.10 List to Json string

@SpringBootTest
public class List2JsonTests {
    @Autowired
    private Person person;
    //List to Json string
    @Test
    void test() {
        List<Person> list = new ArrayList<>();
        person.setName("Zhang San");
        person. setAge(26);
        person. setBirth(new Date());
        list. add(person);
        person = new Person();
        person.setName("Li Si");
        person. setAge(31);
        person. setBirth(new Date());
        list. add(person);
        String listJson = JSON.toJSONString(list);
        System.out.println(listJson);
    }
}

The output is:

2.11Map to Json string

@SpringBootTest
public class Map2JsonTests {
    //Map to Json string
    @Test
    void test(){
         Map<String, Object> map = new HashMap<>();
         map.put("key1","1");
         map.put("key2","2");
         map.put("key3","3");
         map.put("key4","4");
         String mapJson = JSON.toJSONString(map);
        System.out.println("mapJson:" + mapJson);
    }
}

The output is:

  1. Summary

Commonly used Json conversions have been listed, and you can also add some yourself as a summary of applications in daily development. Hand code is not easy, just pay attention.

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Java skill treeHomepageOverview 108548 people are studying systematically