美文网首页
25.jackson与fastjson

25.jackson与fastjson

作者: 若愚同学 | 来源:发表于2018-06-12 22:18 被阅读0次
jackson
SpringMVC默认整合.
jackson-annotations-2.9.2.jar
jackson-core-2.9.2.jar
jackson-databind-2.9.2.jar
(1开头的包对应spring4以前的版本,使用时需要注意)
使用示例代码:
@Test
//测试jackson 单个对象与json之间的相互转换
public void testJacksonSingle() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(new User(1L,"小林",18,new Date()));
    System.out.println(json);
    
    User user = mapper.readValue("{\"id\":1,\"name\":\"小海\"}", User.class);
    System.out.println(user);
}

@Test
//测试jackson 多个对象与json之间的相互转换
public void testJacksonList() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    
    List<User> list = new ArrayList<>();
    list.add(new User(1L,"小林",18,new Date()));
    list.add(new User(2L,"林书",18,new Date()));
    list.add(new User(3L,"若愚",18,new Date()));
    String json = mapper.writeValueAsString(list);
    System.out.println(json);
    
    //javatype,获取类型工厂,用 构造器返回值类型
    //参数1.返回 值类型,参数2.返回值的泛型
    JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, User.class);
    
    List<User> list2 = mapper.readValue(json, javaType);
    for (User user : list2) {
        System.out.println(user);
    }
}
fastjson:
@Test
//测试 fastjson 单个对象与json之间的相互转换
public void testFastJsonSingle() throws Exception {
    String jsonString = JSON.toJSONString(new User(5L,"姬長空",18,new Date()));
    System.out.println(jsonString);
    
    User user = JSON.parseObject(jsonString, User.class);
    System.out.println(user);
}

@Test
//测试 fastjson 单多个对象与json之间的相互转换
public void testFastJsonList() throws Exception {
    List<User> list = new ArrayList<>();
    list.add(new User(1L,"小林",18,new Date()));
    list.add(new User(2L,"林书",18,new Date()));
    list.add(new User(3L,"若愚",18,new Date()));
    String json = JSON.toJSONString(list);
    System.out.println(json);
    
    List<User> list2 = JSON.parseArray(json, User.class);
    for (User user : list2) {
        System.out.println(user);
    }
}

相关文章

  • 25.jackson与fastjson

    jackson 使用示例代码: fastjson:

  • 面试相关

    1.Json解析与XML解析 Gson解析和FastJson ,FastJson解析效率高 XML解析,SAX解析...

  • FastJSON 与克隆

    阿里巴巴的开源工具包FastJSON在处理JSON数据的时候非常好用,然而我最近实习中使用的时候发现了一处非常容易...

  • SpringBoot2 - FastJson

    简介 FastJson是阿里巴巴旗下的一个开源项目,是Json序列化与反序列化组件。 添加FastJson依赖 通...

  • Android Realm 与 FastJson结合的坑

    Android Realm 与 FastJson结合,运行JSON.toJSONString(object)时,报...

  • SpringMVC集成fastjson

    请先参见:fastjson介绍与配置 在SpringMVC中定义FastJsonHttpMessageConver...

  • json转dto

    JSON与List转换 fastJson //list -> json List list = new Arra...

  • 实习 4.0

    com.alibaba.fastjson.JSONArray;com.alibaba.fastjson.JSONO...

  • json字符串转map

    采用fastjson 添加依赖:implementation 'com.alibaba:fastjson:1.1....

  • FastJson混淆的坑

    FastJson 混淆代码 -dontwarn com.alibaba.fastjson.**-keep clas...

网友评论

      本文标题:25.jackson与fastjson

      本文链接:https://www.haomeiwen.com/subject/qcfdeftx.html