FastJson简明上手教程~

本文介绍了阿里巴巴的FastJson库,用于Java对象与JSON字符串之间的转换。通过示例展示了如何将Java对象转换为JSON,List转换为JSON,Map转换为JSON,以及如何从JSON反序列化回Java对象和List、Map。FastJson具有高性能和易用性的特点,在实际开发中广泛应用。
摘要由CSDN通过智能技术生成

简单介绍下~

FastJson是阿里开源的JSON解析库,它可以解析JSON格式的字符串,支持将JAVA Bean序列化JSON字符串,也可以从JSON字符串反序列化到JavaBean。相比其他JSON库而言,FastJson的性能还是不错的,目前在日常开发中使用也非常广泛,且上手也非常简单,支持的功能也非常完备,比如支持泛型、支持流处理超大文本、支持枚举、支持序列化和反序列化扩展。

前戏很重要!

  • 引入FastJson依赖

    <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>fastjson</artifactId>
         <version>1.2.78</version>
    </dependency>
    
  • 创建User类

    // lombok
    @Data
    public class User {
    
        private Long id;
    
        private String username;
    
        private String password;
    
        private Integer sex;
        
        private Integer age;
    
        private String autograph;
    
        private Integer status;
    
        private Date updateTime;
    
        private Date createTime;
    }
    

简单举几个常用例子~

1. Java对象转Json

  • 造点数据
package cn.msjava.coding;

import cn.msjava.coding.eneity.User;
import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * msJava
 *
 * @Description
 * @Date 2022-01-02
 */
public class Main {

    public static void main(String[] args) {

        User user = new User();
        user.setId(1L);
        user.setUsername("admin");
        user.setPassword("admin");
        user.setSex(1);
        user.setAge(18);
        user.setAutograph("风往哪儿吹,不要问跟风的人~");
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());

        String userJson = JSON.toJSONString(user);
        System.out.println(userJson);
    }
}

  • 转换结果
{
	"age": 18,
	"autograph": "风往哪儿吹,不要问跟风的人~",
	"createTime": 1641135506750,
	"id": 1,
	"password": "admin",
	"sex": 1,
	"updateTime": 1641135506750,
	"username": "admin"
}

2. List集合转Json

  • 造点数据
package cn.msjava.coding;

import cn.msjava.coding.eneity.User;
import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * msJava
 *
 * @Description
 * @Date 2022-01-02
 */
public class Main {

    public static void main(String[] args) {

        User user = new User();
        user.setId(1L);
        user.setUsername("admin");
        user.setPassword("admin");
        user.setSex(1);
        user.setAge(18);
        user.setAutograph("风往哪儿吹,不要问跟风的人~");
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());


        User user1 = new User();
        user1.setId(1L);
        user1.setUsername("admin1");
        user1.setPassword("admin1");
        user1.setSex(0);
        user1.setAge(18);
        user1.setAutograph("祖国必然统一~");
        user1.setCreateTime(new Date());
        user1.setUpdateTime(new Date());


        List<User> userList = new ArrayList<User>();
        userList.add(user);
        userList.add(user1);

        String userListJson = JSON.toJSONString(userList);
        System.out.println(userListJson);

    }

}

  • 转换结果
[
  {
	"age": 18,
	"autograph": "风往哪儿吹,不要问跟风的人~",
	"createTime": 1641135971494,
	"id": 1,
	"password": "admin",
	"sex": 1,
	"updateTime": 1641135971494,
	"username": "admin"
  },
  {
	"age": 18,
	"autograph": "祖国必然统一~",
	"createTime": 1641135971494,
	"id": 1,
	"password": "admin1",
	"sex": 0,
	"updateTime": 1641135971494,
	"username": "admin1"
}]

3. Map集合转Json

  • 造点数据
package cn.msjava.coding;

import cn.msjava.coding.eneity.User;
import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * msJava
 *
 * @Description
 * @Date 2022-01-02
 */
public class Main {

    public static void main(String[] args) {

        User user = new User();
        user.setId(1L);
        user.setUsername("admin");
        user.setPassword("admin");
        user.setSex(1);
        user.setAge(18);
        user.setAutograph("风往哪儿吹,不要问跟风的人~");
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        User user1 = new User();
        user1.setId(1L);
        user1.setUsername("admin1");
        user1.setPassword("admin1");
        user1.setSex(0);
        user1.setAge(18);
        user1.setAutograph("祖国必然统一~");
        user1.setCreateTime(new Date());
        user1.setUpdateTime(new Date());

        HashMap<String, User> userMap = new HashMap<String, User>();
        userMap.put("user",user);
        userMap.put("user1",user1);

        String userMapJson = JSON.toJSONString(userMap);
        System.out.println(userMapJson);
    }
}
  • 转换结果
{
	"user1": {
		"age": 18,
		"autograph": "祖国必然统一~",
		"createTime": 1641173214065,
		"id": 1,
		"password": "admin1",
		"sex": 0,
		"updateTime": 1641173214065,
		"username": "admin1"
	},
	"user": {
		"age": 18,
		"autograph": "风往哪儿吹,不要问跟风的人~",
		"createTime": 1641173214065,
		"id": 1,
		"password": "admin",
		"sex": 1,
		"updateTime": 1641173214065,
		"username": "admin"
	}
}

4. Json转Java对象

  • 造点数据
package cn.msjava.coding;

import cn.msjava.coding.eneity.User;
import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * msJava
 *
 * @Description
 * @Date 2022-01-02
 */
public class Main {

    public static void main(String[] args) {
          String userJson="{\n" +
                  "\t\"age\": 18,\n" +
                  "\t\"autograph\": \"风往哪儿吹,不要问跟风的人~\",\n" +
                  "\t\"createTime\": 1641135506750,\n" +
                  "\t\"id\": 1,\n" +
                  "\t\"password\": \"admin\",\n" +
                  "\t\"sex\": 1,\n" +
                  "\t\"updateTime\": 1641135506750,\n" +
                  "\t\"username\": \"admin\"\n" +
                  "}";

        User user = JSON.parseObject(userJson, User.class);
        System.out.println(user.toString());
    }
}

  • 转换结果
User(id=1, username=admin, password=admin, sex=1, age=18, autograph=风往哪儿吹,不要问跟风的人~, status=null, updateTime=Sun Jan 02 22:58:26 CST 2022, createTime=Sun Jan 02 22:58:26 CST 2022)

5. Json转List集合

  • 造点数据
package cn.msjava.coding;

import cn.msjava.coding.eneity.User;
import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * msJava
 *
 * @Description
 * @Date 2022-01-02
 */
public class Main {

    public static void main(String[] args) {
          String userListJson="[\n" +
                  "  {\n" +
                  "\t\"age\": 18,\n" +
                  "\t\"autograph\": \"风往哪儿吹,不要问跟风的人~\",\n" +
                  "\t\"createTime\": 1641135971494,\n" +
                  "\t\"id\": 1,\n" +
                  "\t\"password\": \"admin\",\n" +
                  "\t\"sex\": 1,\n" +
                  "\t\"updateTime\": 1641135971494,\n" +
                  "\t\"username\": \"admin\"\n" +
                  "  },\n" +
                  "  {\n" +
                  "\t\"age\": 18,\n" +
                  "\t\"autograph\": \"祖国必然统一~\",\n" +
                  "\t\"createTime\": 1641135971494,\n" +
                  "\t\"id\": 1,\n" +
                  "\t\"password\": \"admin1\",\n" +
                  "\t\"sex\": 0,\n" +
                  "\t\"updateTime\": 1641135971494,\n" +
                  "\t\"username\": \"admin1\"\n" +
                  "}]";

        List<User> userList = JSON.parseArray(userListJson, User.class);
        userList.forEach(x->{
            System.out.println(x);
        });
    }
}
  • 转换结果
User(id=1, username=admin, password=admin, sex=1, age=18, autograph=风往哪儿吹,不要问跟风的人~, status=null, updateTime=Sun Jan 02 23:06:11 CST 2022, createTime=Sun Jan 02 23:06:11 CST 2022)
User(id=1, username=admin1, password=admin1, sex=0, age=18, autograph=祖国必然统一~, status=null, updateTime=Sun Jan 02 23:06:11 CST 2022, createTime=Sun Jan 02 23:06:11 CST 2022)

6. Json转Map集合

  • 造点数据
package cn.msjava.coding;

import cn.msjava.coding.eneity.User;
import com.alibaba.fastjson.JSONObject;
import java.util.*;

/**
 * msJava
 *
 * @Description
 * @Date 2022-01-02
 */
public class Main {

    public static void main(String[] args) {
        String userJson = "{\n" +
                "\t\"user1\": {\n" +
                "\t\t\"age\": 18,\n" +
                "\t\t\"autograph\": \"祖国必然统一~\",\n" +
                "\t\t\"createTime\": 1641173214065,\n" +
                "\t\t\"id\": 1,\n" +
                "\t\t\"password\": \"admin1\",\n" +
                "\t\t\"sex\": 0,\n" +
                "\t\t\"updateTime\": 1641173214065,\n" +
                "\t\t\"username\": \"admin1\"\n" +
                "\t},\n" +
                "\t\"user\": {\n" +
                "\t\t\"age\": 18,\n" +
                "\t\t\"autograph\": \"风往哪儿吹,不要问跟风的人~\",\n" +
                "\t\t\"createTime\": 1641173214065,\n" +
                "\t\t\"id\": 1,\n" +
                "\t\t\"password\": \"admin\",\n" +
                "\t\t\"sex\": 1,\n" +
                "\t\t\"updateTime\": 1641173214065,\n" +
                "\t\t\"username\": \"admin\"\n" +
                "\t}\n" +
                "}";

        Map<String, User> userMap = (Map)JSONObject.parse(userJson);

        for (Map.Entry<String, User> entry: userMap.entrySet()){
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }

    }
}
  • 转换结果
user1
{"password":"admin1","createTime":1641173214065,"sex":0,"autograph":"祖国必然统一~","updateTime":1641173214065,"id":1,"age":18,"username":"admin1"}
user
{"password":"admin","createTime":1641173214065,"sex":1,"autograph":"风往哪儿吹,不要问跟风的人~","updateTime":1641173214065,"id":1,"age":18,"username":"admin"}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值