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

image-20211205005649862

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 简单使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zeeland

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值