• 使用json进行序列化


    package com.example.demo01;

    import com.fasterxml.jackson.core.JsonFactory;
    import com.fasterxml.jackson.core.JsonGenerator;
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.ObjectMapper;

    import java.io.IOException;
    import java.io.StringWriter;
    import java.util.Date;

    /**
    * @createTime 2020年11月27日 21:06:00
    */
    public class TestJSONSerialization {

    public static void main(String[] args) throws IOException {

    Person person = new Person();
    person.setAddress("hangzhou,china");
    person.setAge(18);
    person.setBirth(new Date());
    person.setName("zhangsan");

    //json对象序列化
    String personJson = null;
    ObjectMapper mapper = new ObjectMapper();
    StringWriter sw = new StringWriter();
    JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
    mapper.writeValue(gen, person);
    gen.close();
    personJson = sw.toString();

    //json对象反序列化
    Person zhangsan = (Person)mapper.readValue(personJson, Person.class);

    System.out.println(personJson);
    System.out.println(zhangsan.getName());
    }
    }
  • 相关阅读:
    3-百度网盘视频在线倍速播放
    16-算法训练 数字三角形
    【2018ACM/ICPC网络赛】徐州赛区
    【python】collections的使用
    【python】遇到的错误
    【2018ACM/ICPC网络赛】沈阳赛区
    【csp】2018-3
    【python】快速排序
    【csp】2017-12
    【校OJ】选网线
  • 原文地址:https://www.cnblogs.com/xzcBY/p/14050355.html
Copyright © 2020-2023  润新知