20220530Java在Redis里用FastJson存取封装对象Object

本文介绍了如何使用FastJson将Java对象转换为Json格式存储到Redis,并从Redis取出时转换回Object类型。通过`JSONObject.toJSONString(Object obj)`方法将对象转成Json字符串,再利用`JSON.parseObject(String text, Class claz)`或`JSON.parseArray(String text, Class claz)`解析Json字符串为对象或列表。" 5048573,715667,使用SOCK_PACKET构建ARP请求程序详解,"['网络编程', '数据结构', 'C语言', '套接字']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Background

If I save the content as String type into Redis, the matter is that how I can save as Json format and how to get an Object.class when we take out the text content from Redis?

My friend told me that using FastJson to save Object into Redis,then take out then with the format of json:

image-20220530010945715

  • DTO bean
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Description:
 * @author: Spike Wong
 * @Created: 2022/5/15
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserLoginDTO {
    private String userName;
    private String password;
}

Example 1: JSON.parseObject

Here I use JSONObject.toJSONString(Object obj) to make an Object changed to JsonString,

then, to take out and transfer it into an Object type by using JSON.parseObject(@Nullable String text,Class claz).

Codes reference as below:

   @Test
    void test1(){
        stringRedisTemplate.opsForValue().set("user:1054", JSONObject.toJSONString(new UserLoginDTO("kiseki", "12345678")));
        String s = stringRedisTemplate.opsForValue().get("user:1054");
        System.out.println(s);
        UserLoginDTO userLoginDTO = JSON.parseObject(s, UserLoginDTO.class);
        System.out.println(userLoginDTO);
    }

//{"password":"12345678","userName":"kiseki"}
//UserLoginDTO(userName=kiseki, password=12345678)

image-20220530002604083

Example 2:JSON.parseArray

If there is some needs requiring json to contain a list type.It should be used by JSON.parseObject(s @Nullable String text,Class claz).

Then we will use JSON.parseArray(@Nullable String text,Class claz) to transfer the json to a List.
Codes reference as below:

    @Test
    void test2() {
        List<UserLoginDTO> list = new ArrayList<>();
        list.add(new UserLoginDTO("root1", "1314520"));
        list.add(new UserLoginDTO("root2", "1314521"));
        list.add(new UserLoginDTO("root3", "5201314"));
        list.add(new UserLoginDTO("root4", "5211314"));
        System.out.println(list);
        //then we put the list  into redis with String Type by JSONObject.toJSONString()
        stringRedisTemplate.opsForValue().set("user:blackList:may", JSONObject.toJSONString(list));
        String s = stringRedisTemplate.opsForValue().get("user:blackList:may");
        System.out.println(s);
        //transfer into DTO lists
        List<UserLoginDTO> userLoginDTOS = JSON.parseArray(s, UserLoginDTO.class);
        System.out.println(userLoginDTOS);
    }

//System.out.println(list):
//[UserLoginDTO(userName=root1, password=1314520), UserLoginDTO(userName=root2, password=1314521), UserLoginDTO(userName=root3, password=5201314), UserLoginDTO(userName=root4, password=5211314)]


//System.out.println(s):
//[{"password":"1314520","userName":"root1"},{"password":"1314521","userName":"root2"},{"password":"5201314","userName":"root3"},{"password":"5211314","userName":"root4"}]


//System.out.println(userLoginDTOS):
//[UserLoginDTO(userName=root1, password=1314520), UserLoginDTO(userName=root2, password=1314521), UserLoginDTO(userName=root3, password=5201314), UserLoginDTO(userName=root4, password=5211314)]

//we can see that the content of userLoginDTOS is same as list's.

We can see that the content of userLoginDTOS is same as list’s through the comparison !

image-20220530011105942

Note:Sometimes,there are some accidents with serialization and deserialization,especially in the situation of cross-language working space.It seems that it’s necessary to operate in this way~~~~

有些情况反序列化的时候会出问题,特别是跨语言!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值