Jackson和FastJSON 快速上手

Posted 帅气的黑桃J

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jackson和FastJSON 快速上手相关的知识,希望对你有一定的参考价值。

简介

本教程提供了Jackson和FastJSON快速上手的示例,Jackson和FastJSON都是JSON处理的工具,但是笔者认为用FastJSON操作会更方便很多,在速度上也更具优势,因此首先推荐使用FastJSON。

友情提示:笔者所做的工作仅为的整合工作,因此本篇博客仅仅展示笔者成功调试的过程,仅供参考。

Jackson官方文档

FastJSON快速上手

依赖与开发前准备

首先我们导入FastJSON的maven依赖。

<dependencies>
    
    <!--   Fastjson     -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.75</version>
    </dependency>

    <!--    lombok    -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>RELEASE</version>
        <scope>compile</scope>
    </dependency>

</dependencies>

然后我们用Lombok设置一个简单的Java类。

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;


@Data
@AllArgsConstructor
@NoArgsConstructor
public class Friend 
    private String nickname;
    private int age;

将Map转成JSON

@Test
public void MapConvertToJSonTest() throws Exception 
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("key1", "One");
    map.put("key2", "Two");

    String mapJson = JSON.toJSONString(map);
    JSON json = (JSON)JSON.toJSON(map);
    System.out.println(mapJson);


运行结果

"key1":"One","key2":"Two"

将List转成JSON

@Test
public void ListConvertToObjectTest() throws Exception 
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

    Map<String, Object> map1 = new HashMap<String, Object>();
    map1.put("key1", "One");
    map1.put("key2", "Two");

    Map<String, Object> map2 = new HashMap<String, Object>();
    map2.put("key1", "Three");
    map2.put("key2", "Four");

    list.add(map1);
    list.add(map2);

    String listJson = JSON.toJSONString(list);
    System.out.println(listJson);

运行结果

["key1":"One","key2":"Two","key1":"Three","key2":"Four"]

JavaBean转JSON

@Test
public void JavaBeanConvertToJSON()throws Exception
    Friend friend = new Friend("jack",18);
    String beanJson = JSON.toJSONString(friend);
    System.out.println(beanJson);

运行结果

"age":18,"nickname":"jack"

格式化输出JSON

只要在JSON.toJSONString函数中的第二个形参添加true即可JSON.toJSONString(javaBean,true)

@Test
public void JavaBeanConvertToJSON()throws Exception
Friend friend = new Friend("jack",18);
String beanJson = JSON.toJSONString(friend,true);
System.out.println(beanJson);

格式化输出即输出如下图的json

JSON转JavaBean

方法1

@Test
public void JSONConvertToJavaBean() throws Exception 
    Friend friend = new Friend("jack",18);
    String beanJson = JSON.toJSONString(friend);
    Friend myFriend = JSON.parseObject(beanJson,Friend.class);
    System.out.println(myFriend);

运行结果

Friend(nickname=jack, age=18)

JSON转List

@Test
public void JSONtoList()
    // json转list
    Friend friend1 = new Friend("jack",18);
    Friend friend2 = new Friend("JSON",20);
    List<Friend> friends = new ArrayList<>();
    friends.add(new Friend("jack",18));
    friends.add(new Friend("JSON",20));
    String jsonString = JSON.toJSONString(friends);
    System.out.println(jsonString);
    //jsonString转BeanList
    List<Friend> list = JSON.parseArray(jsonString,Friend.class);
    for (Friend friend : list) 
        System.out.println(friend);
    

运行结果

["age":18,"nickname":"jack","age":20,"nickname":"JSON"]
Friend(nickname=jack, age=18)
Friend(nickname=JSON, age=20)

Jackson快速上手

依赖与开发前准备

首先我们导入Jackson的maven依赖。

<dependencies>
    
    <!--   jackson     -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.4</version>
    </dependency>

    <!--    lombok    -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>RELEASE</version>
        <scope>compile</scope>
    </dependency>

</dependencies>

然后我们用Lombok设置一个简单的Java类。

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;


@Data
@AllArgsConstructor
@NoArgsConstructor
public class Friend 
    private String nickname;
    private int age;

JavaBean转JSON字符串

@Test
public void ObjectToJSonTest() throws Exception 
    ObjectMapper mapper = new ObjectMapper();
    Friend friend = new Friend("yitian", 25);
    String text = mapper.writeValueAsString(friend);
    System.out.println("[info] text is :"+text);

运行结果

[info] text is :"nickname":"yitian","age":25

JSON解析

   @Test
    public void JsonToObjectTest() throws Exception 
        ObjectMapper mapper = new ObjectMapper();

        //创建JSON实例
        Map<String, Object> map = new HashMap<>();
        map.put("age", 25);
        map.put("name", "yitian");
        map.put("interests", new String[]"pc games", "music");

        //JSON字符串
        String text = mapper.writeValueAsString(map);
        System.out.println("[info] JSON data:"+text);

        Map<String, Object> map2 = mapper.readValue(text, new TypeReference<Map<String, Object>>() 
        );

        //JSON数据解析
        JsonNode root = mapper.readTree(text);
        String name = root.get("name").asText();
        int age = root.get("age").asInt();

        Friend friend = new Friend(name,age);
        System.out.println("[info] java object data :"+friend);
    

运行结果

[info] JSON data:"name":"yitian","interests":["pc games","music"],"age":25
[info] java object data :Friend(nickname=yitian, age=25)

参考资料

Jackson快速入门

FastJSON 简单使用

以上是关于Jackson和FastJSON 快速上手的主要内容,如果未能解决你的问题,请参考以下文章

Jackson和FastJson性能谁更快

JSON工具类(FastJson,Gson,Jackson)

fastjson和jackson使用对比

Jackson--Jackson,fastjson和json-lib

fastjson--Jackson,fastjson和json-lib

Jackson替换fastjson的几个坑