Jackson、Gson、Fastjson 序列化与反序列化使用简介

Java序列化是指把Java对象转换为可传输的字节序列的过程。
Java反序列化是指把传输的字节序列恢复为Java对象的过程。
这两个过程使我们非常方便的存储和传输数据。

准备一个java bean Person 用来序列化和反序列化操作

package com.example.json;

/**
 * author:xyb
 * Date:2019-08-28 上午 11:09
 */
public class Persion {
    private  String name;
    private  int age;

    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;
    }
}


一、Gson

  • 引入Maven / Gradle依赖:

  com.google.code.gson
  gson
  2.8.5

implementation 'com.google.code.gson:gson:2.8.5'
  • Gson序列化Map对象Java beanList
    @Test
    public void toJSON(){
        HashMap map = new HashMap();
        map.put("name","gson");
        map.put("age","24");

        Person person = new Person();
        person.setName("jackson");
        person.setAge(24);

        // 创建Gson对象
        Gson gson = new Gson();

        // 1、map 序列化成 Json字符串
        String json = gson.toJson(map);
        System.out.println(json);

        // 2、Java Bean 序列化成 Json字符串
        String personJson = gson.toJson(person);
        System.out.println(personJson);

        // 3、List 集合序列化成 Json数组: 将两个Person bean添加到List中
        Person person1 = new Person();
        person1.setName("gson");
        person1.setAge(34);
        ArrayList personList = new ArrayList<>();
        personList.add(person);
        personList.add(person1);

        String personListJson = gson.toJson(personList);
        System.out.println(personListJson)
    }

  • Gson 反序列化Map对象Java bean
    @Test
    public void  fromJSON(){
        // Json字符串
        String xiaoming = "{\"name\":\"gson\",\"age\":\"24\"}";

        // 创建Gson对象
        Gson gson = new Gson();

        // 1、Json对象反序列化成Map
        Map result = gson.fromJson(xiaoming, new TypeToken() { }.getType());
        System.out.println(result);

        // 2、Json对象反序列化成Java Bean
        Person person = gson.fromJson(xiaoming, Person.class);
        System.out.println(person);
    }


二、Fastjson

引入Maven 或 Gradle依赖:


    com.alibaba
    fastjson
    1.2.61

compile 'com.alibaba:fastjson:1.2.61'
  • Fastjson 序列化Map对象Java beanList
    @Test
    public void Fastjson(){
        Person person = new Person();
        person.setName("xie");
        person.setAge(24);

        // Java Bean序列化从json对象
        String s = JSON.toJSONString(person);
        System.out.println(s);

        // 从Map序列化成Json
        Map map = new HashMap<>();
        map.put("name", "xiexie");
        map.put("age", 26);
        String jsonString = JSON.toJSONString(map);
        System.out.println(jsonString);

        //List转json数组
        Person person1 = new Person();
        Person person2 = new Person();
        person1.setName("xie");
        person2.setName("zhang");
        person1.setAge(26);
        person2.setAge(24);

        List users = new ArrayList();
        users.add(person1);
        users.add(person2);

        String ListUserJson = JSON.toJSONString(users);
        System.out.println(ListUserJson);

    }
 
 
  • Fastjson 反序列化Map对象Java bean
    @Test
    public void toFastjson(){
        // Json 字符串
        String xiaoming = "{\"name\":\"fastjson\",\"age\":\"24\"}";
        String personListJson = "{\"userList\":[{\"name\":\"jackson\",\"age\":\"24\"},{\"name\":\"gson\",\"age\":\"25\"}]}";

        // 1、Json字符串序列化成Map 先将字符串转换成json对象,再将json对象转Map
        JSONObject  jsons= JSONObject.parseObject(xiaoming);

        Map map = (Map)jsons;
        System.out.println("age的值是"+map.get("age"));

        // 2、Json字符串序列化成Java Bean
        Person jsonObject = JSON.parseObject(xiaoming,Person.class);
        System.out.println(jsonObject.getName());

        // 3、Json字符串序列化成List
    }


三、Jackson

  • Jackson 序列化Map对象Java beanList
    @Test
    public void toJson() throws IOException {

        Person person = new Person();
        person.setName("jackson");
        person.setAge(24);

        HashMap map = new HashMap();
        map.put("name","gson");
        map.put("age","24");

        // 创建Jackson对象
        ObjectMapper objectMapper = new ObjectMapper();

        // 1、map 序列化成 Json对象
        String s = objectMapper.writeValueAsString(map);
        System.out.println(s);

        // 2、Java Bean 序列化成 Json对象
        String userJson = objectMapper.writeValueAsString(person);
        System.out.println(userJson);

        // 3、List 集合序列化成 Json数组: 将两个Person bean添加到List中
        Person person1 = new Person();
        person1.setName("gson");
        person1.setAge(34);
        ArrayList personList = new ArrayList<>();
        personList.add(person);
        personList.add(person1);

        String personListJson = objectMapper.writeValueAsString(personList);
        System.out.println(personListJson);

    }
  • Jackson 反序列化Map对象Java bean
    @Test
    public void jacksonBean() throws IOException {

        // Json 字符串
        String xiaoming = "{\"name\":\"gson\",\"age\":\"24\"}";
        String personListJson = "{\"userList\":[{\"name\":\"jackson\",\"age\":\"24\"},{\"name\":\"gson\",\"age\":\"25\"}]}";

        // 创建Jackson对象
        ObjectMapper objectMapper = new ObjectMapper();

        // 1、Json字符串反序列化成 Map
        Map jacksonmap = objectMapper.readValue(xiaoming, Map.class);
        System.out.println(jacksonmap);

        // 2、Json字符串反序列化成 Java Bean
        com.example.json.pojo.Person person = objectMapper.readValue(xiaoming, com.example.json.pojo.Person.class);
        System.out.println(person);

        // 3、Json字符串数组反序列化成 List

    }
  • json对象转map
String data = {"admin":"1","史新国":"2","谢亚波":"3","张鹏":"4","王璞":"5"}

ObjectMapper objectMapper = new ObjectMapper();

Map m = objectMapper.readValue(a, Map.class); //json转换成map

转换后的map:{admin=1, 史新国=2, 谢亚波=3, 张鹏=4, 王璞=5}

你可能感兴趣的:(Jackson、Gson、Fastjson 序列化与反序列化使用简介)